RE: Dynamic image resizing on upload

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



$Filename = full path and filename of original file
$Thumbnail = full path and filename of thumbnail
$WIDTH && $HEIGHT = specified for resizing

Gdlib must be installed.

<?php
function Resize($Filename,$Thumbnail,$WIDTH,$HEIGHT){
		$image = $Filename;
		$thumbnail = $Thumbnail;
		$imagesize = getimagesize($image);
		$fwidth = $imagesize[0]; 
		$fheight = $imagesize[1];
		
		if($fheight <= $fwidth){
			$percentage = round(($WIDTH/$fwidth) * 100);
			$newwidth  = round(($fwidth  * $percentage)/100);
			$newheight = round(($fheight * $percentage)/100);
		}
		elseif($fheight > $fwidth){
			$percentage = round(($HEIGHT/$fheight) * 100);
			$newwidth  = round(($fwidth  * $percentage)/100);
			$newheight = round(($fheight * $percentage)/100);
		}
		else{
			$percentage = round(($fwidth/$WIDTH) * 100);
			$newwidth  = round(($fwidth  * $percentage)/100);
			$newheight = round(($fheight * $percentage)/100);
		}
		$destImage = imagecreatetruecolor($newwidth,$newheight) or
die($file.' - '.$newwidth.':'.$newheight);
		$type = strtolower(substr($Filename,-3));
		switch($type){
			case 'gif':
				$srcImage = ImageCreateFromGif($image);
				ImageCopyResized($destImage, $srcImage, 0,
0, 0, 0, $newwidth, $newheight, imagesx($srcImage), imagesy($srcImage));
				ImageGif($destImage,$thumbnail);
			break;
			case 'jpg':
				$srcImage = ImageCreateFromJPEG($image);
				ImageCopyResized($destImage, $srcImage, 0,
0, 0, 0, $newwidth, $newheight, imagesx($srcImage), imagesy($srcImage));
				ImageJPEG($destImage,$thumbnail);
			break;
			case 'png':
				$srcImage = ImageCreateFromPNG($image);
				ImageCopyResized($destImage, $srcImage, 0,
0, 0, 0, $newwidth, $newheight, imagesx($srcImage), imagesy($srcImage));
				ImagePNG($destImage,$thumbnail);
			break;
		}
		//free the memory used for the images
		ImageDestroy($srcImage);
		ImageDestroy($destImage);
		
		if(file_exists($thumbnail)){
			return true;
		}
		else{
			return false;
		}
	}?>

-----Original Message-----
From: Mark Abrams [mailto:mark@xxxxxxxxxxxxxxxxxxxxxxxx] 
Sent: 12 June 2007 03:39 PM
To: php-windows@xxxxxxxxxxxxx
Subject:  Dynamic image resizing on upload

Is there a PHP function to resize a user's JPG or GIF image during an 
upload?

I was advised to use Image Magick  http://www.imagemagick.org   However, the

Windows install doc has not been written.   I have unsuccessfully struggled 
with the install for days ...

All I want to do is to resize  a large image to a thumbnail of my dimensions

when my end user uploads a an image.

TIA for any help. 

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




***********************************************************************************************
The information contained in this e-mail is confidential and may be subject to legal privilege. 
Access to this e-mail by anyone other than the intended recipient is unauthorised.

If you are not the intended recipient you must not use, copy, distribute or disclose the e-mail or any part of its contents or take any action in reliance on it. If you have received this e-mail in error, please notify us immediately by e-mail (postmaster@xxxxxxxxxxxxxxxxxxxxxx) or telephone (+27 11 265 4200).
This message is free of all known viruses. It has been screened for viruses by Blockmail.
***********************************************************************************************


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux