Jump to content

resizing my images only works for JPG ?


jasonc

Recommended Posts

I have the following code that seems to only work for JPG files.

I would really like to allow visitors to upload any type of file, but i still need to have the option to resize them if they are to big. (more than 500x500)

I have just looked at my code and seen that it will only resize a JPG file anyway. (imagecreatefromjpeg)

Is there a standard PHP resize function that can resize any image?


Thank you in advance for your help.




<?
function resize_jpeg_image($src_image, $dst_image, $max_width, $max_height)
{
if (!($src_img = imagecreatefromjpeg($src_image)))
return 0;
if (imagesx($src_img) > $max_width || imagesy($src_img) > $max_height)
{
$origwidth = imagesx($src_img);
$origheight = imagesy($src_img);
if ($max_height / $origheight < $max_width / $origwidth) { $scale = $max_height / $origheight; }
if ($max_width / $origwidth < $max_height / $origheight) { $scale = $max_width / $origwidth; }
$height = $scale * $origheight;
$width = $scale * $origwidth;

if (!($dst_img = imagecreatetruecolor($width, $height))
|| !imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $origwidth, $origheight)
|| !imagejpeg($dst_img, $dst_image )
|| !imagedestroy($dst_img))
return 0;
}
else if (!copy($src_image, $dst_image))
return 0;

return 1;
}
?>
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.