Jump to content

image resize


envexlabs

Recommended Posts

You need the GD lib installed for this to work (look in phpinfo() to see if you do):

 

function img_resize($image) {
  $filename = $image;
  header('Content-type: image/jpeg');
  $percent = 0.22;
  list($width, $height) = getimagesize($filename);
  $newwidth = $width * $percent;
  $newheight = $height * $percent;
  $thumb = imagecreatetruecolor($newwidth, $newheight);
  $source = imagecreatefromjpeg($filename);
  imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  imagejpeg($thumb);
}

 

So you use it like so:

img_resize("/images/large_pics/pic1028489.jpg");

 

Yeah and it only works on JPGs too, though you can fix this by using imagecreatefromgif($filename); and imagecreatefrompng($filename); if you want.

Link to comment
https://forums.phpfreaks.com/topic/67110-image-resize/#findComment-336564
Share on other sites

did you print/echo anything out after the function call?

 

that would break the page... what you need to do is something like this:

 

"img_resize.php":

img_resize("myImage.jpg");
function img_resize($image) {
  $filename = $image;
  header('Content-type: image/jpeg');
  $percent = 0.22;
  list($width, $height) = getimagesize($filename);
  $newwidth = $width * $percent;
  $newheight = $height * $percent;
  $thumb = imagecreatetruecolor($newwidth, $newheight);
  $source = imagecreatefromjpeg($filename);
  imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  imagejpeg($thumb);
}

 

Then in "myPage.php" something like:

<img src='img_resize.php" />

 

Hope that helps.

 

Link to comment
https://forums.phpfreaks.com/topic/67110-image-resize/#findComment-336708
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.