Jump to content

Resizing Image With Php. Please Help.


tomahawk

Recommended Posts

Hey guys, I have built a photo gallery using flex builder 3. I am allowing a user to upload photos to be displayed in the photo album. The user needs to upload an image that is 680 x 480. Currently I have php adding this photo to a folder. What I need to do is use php to perform two more tasks. I would like the php to take that photo and scale it down to a thumbnail image that I need for the photogallery. The thumbnail image needs to be 70 x 70. Therefore, the php has to scale down the image from 680 x 480 to 70 x 70 and then its only a matter of me having php save a new image as the thumbnail into that folder.Any help is much appreciated.Thanks.

Link to comment
Share on other sites

I've got a function that does just that, this is the function:

function resize_image($opts){  $src = isset($opts['source']) ? $opts['source'] : '';  $dest = isset($opts['dest']) ? $opts['dest'] : '';  $w = isset($opts['w']) ? intval($opts['w']) : 0;  $h = isset($opts['h']) ? intval($opts['h']) : 0;    if ($src == '')  {	return;  }  if ($w == 0 && $h == 0)  {	return;  }  if ($dest == '')	$dest = $src; // resize in place  // open the image  $ext = strtolower(array_pop(explode('.', $src)));  switch ($ext)  {	case 'jpg':	case 'jpeg':	  $i = imagecreatefromjpeg($src);	break;	case 'gif':	  $i = imagecreatefromgif($src);	break;	case 'png':	  $i = imagecreatefrompng($src);	break;	default:	  return;  }  $new_w = imagesx($i);  $new_h = imagesy($i);  if (($w != 0 && $new_w < $w && $h == 0) ||	  ($w == 0 && $h != 0 && $new_h < $h) ||	  ($w != 0 && $new_w < $w && $h != 0 && $new_h < $h))  {	// image is small enough	if ($dest != $src)	  copy($src, $dest);	return;  }  // determine new size  if ($w != 0 && $new_w > $w)  {	$new_h = ($w / $new_w) * $new_h;	$new_w = $w;  }  if ($h != 0 && $new_h > $h)  {	$new_w = ($h / $new_h) * $new_w;	$new_h = $h;  }    // resize  $new = imagecreatetruecolor($new_w, $new_h);  imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i));  imagedestroy($i);  // save the image  switch ($ext)  {	case 'jpg':	case 'jpeg':	  imagejpeg($new, $dest);	break;	case 'gif':	  imagegif($new, $dest);	break;	case 'png':	  imagepng($new, $dest);	break;  }  imagedestroy($new);}

You would use it like this:

resize_image(array(  'source' => '/path/to/source.jpg',  'dest' => '/path/to/thumb.jpg',  'w' => 70,  'h' => 70));

That will only resize smaller, not larger. It will also resize in proportion, if you start with an image that's 700x100 it will resize it to 70x10, not 70x70.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...