Forum Moderators: coopster

Message Too Old, No Replies

upload and resize image

php resize upload image

         

sumobaby

1:13 pm on May 2, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm uploading and resizing images so that they fit a maximum height or width, but the problem I'm having is that the quality of the image is really rubbish after it's been resized. Has anyone got any ideas of how to fix that or have experienced the same problem?

code:

<?php

$full_url = "../posterFull/".$id.".jpg";

$imageInfo = getimagesize($imgfile);
$src_width = $imageInfo[0];
$src_height = $imageInfo[1];

$max = checkMax($src_width, $src_height, 340);

($max == 2)? $max = 0 : $max = 1;
$divide = $imageInfo[$max] / 340;
$dest_width = $src_width / $divide;
$dest_height = $src_height / $divide;

$src_img = imagecreatefromjpeg($imgfile);
$dst_img = imagecreate($dest_width,$dest_height);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
imagejpeg($dst_img,$full_url);
imagedestroy($src_img);
?>

If anyone can help I'd be much appreciated as I thought this upload thing was under control and I've already wasted 4 hours trying to figure out why it comes out so rubbish on a bank holiday.

dreamcatcher

5:26 pm on May 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Instead of using 'imagecreate', have you tried 'imagecreatetruecolor'?

dc

sumobaby

6:20 pm on May 2, 2005 (gmt 0)

10+ Year Member



Yes, I've only tried imagecreate recently because I saw someone else do it elsewhere.

Thanks for replying

jusdrum

6:35 pm on May 2, 2005 (gmt 0)

10+ Year Member



Instead of imagecopyresized, use imagecopyresampled. That should fix your problem.

Longhaired Genius

8:12 pm on May 2, 2005 (gmt 0)

10+ Year Member



Resizing is a quick and dirty way to reduce a picture's size. Resampling gives better results but takes more server effort. Try imagecopyresampled()

Wi11

8:14 pm on May 2, 2005 (gmt 0)

10+ Year Member



Might be worth checking if your host has ImageMagick installed, that provides excellent quality.

sumobaby

5:58 pm on May 3, 2005 (gmt 0)

10+ Year Member



the server on which this problem is occuring doesn't have ImageMagick, it's proper rubbish.

Will try imagecopyresampled shortly, thank you.

sumobaby

6:03 pm on May 3, 2005 (gmt 0)

10+ Year Member



I think imagecopyresampled has just fixed all my woes. Thank you all so much for replying and helping out. I am so relieved.

...did I say thank you?