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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.