Problem resizing pictures !!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Problem resizing pictures !!

Post by Perfidus »

Hello everybody!
I'm using the following code to upload and resize some JPEG pictures to a web, the problem is that the resized pictures quality is too bad.
Is there any instruction for the gd or whatever to keep the quality as better as it is?? It seems like the compression ratio is increased, I would like it to keep it really low (0% should be excellent).

Code: Select all

$im1 = ImageCreateFromString($str);
	$imgname = $Ref."thumb_1";	// Nombre imagen (se podría indicar desde el formulario)
	$maxwidth = 300;		// Ancho máximo
	$maxheight = 150;	// Alto máximo

	$width1 = ImageSX($im1);
	$height1 = ImageSY($im1);
	$width2 = $maxwidth;
	$height2 = floor(($width2 * $height1) / $width1);

	if($maxheight > 0 && $height2 > $maxheight) {

		$height2 = $maxheight;
		$width2 = floor(($height2 * $width1) / $height1);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the code posted does not do resizing.

imagecreatetruecolor() > imagecopyresampled() > imagejpeg(.., quality)
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

You are right and you gave me the right hint, thanks a lot, I have change the quality attribute to '100' and I hope it works!
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

I have set the quality in Imagejpeg to 100% but it still reduces the density so much, compression seem to be up to 50%. Any hints?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

If you have GD version 2.0 I believe.. use imagecreatetruecolor. I had this same probably before and switching to imagecreatetruecolor saved me.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

isn't that what I said in my post? yes.
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

Yeah, you both right, but it still sucks when I try to place some text over the picture:

Code: Select all

$im = imagecreatefromJpeg("full/".$Ref.".jpg");
if ($color==rojo){$textcolor = imagecolorallocate($im,255, 0, 0);}
else if($color==verde){$textcolor = imagecolorallocate($im,0, 255, 0);}
else if ($color==azul){$textcolor = imagecolorallocate($im,0, 0, 255);}
else if ($color==negro){$textcolor = imagecolorallocate($im,0, 0, 0);}
else if ($color==blanco){$textcolor = imagecolorallocate($im,255, 255, 255);}
else if ($color==amarillo){$textcolor = imagecolorallocate($im,255, 255, 0);}
imagettftext($im, $font_size, $angle, $x_start, $y_start, $textcolor, $font_file, $texto);
ImageJpeg($im, "full/".$Ref.".jpg", '100');
In this case I do not know how to introduce the imagecreatetruecolor() statement...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pseudocode-ish

Code: Select all

imagecreatetruecolor a destination image
imagecreatefromjpeg the original source
imagecopyresampled from source to destination
allocate color on destination
add your text
output jpeg
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

First I have substitute ImageCopyResampled by Imagecopy because I do not need to resize in this case, after I have add the Imagecreatetruecolor but it doesn't seem to work, or at least, it doesn't seem to really left the quality of the JPG to 100% but smaller...

Code: Select all

$imdef = ImageCreateTrueColor($imagewidth, $imageheight);
$im = imagecreatefromJpeg("full/".$Ref.".jpg");
//ImageCopyResampled($imdef, $im, 0, 0, 0, 0, $imagewidth, $imageheight, $imagewidth, $mageheight);
imagecopy ( $imdef, $im, 0, 0, 0, 0, $imagewidth, $imageheight);

if ($color==rojo){$textcolor = imagecolorallocate($imdef,255, 0, 0);}
else if($color==verde){$textcolor = imagecolorallocate($imdef,0, 255, 0);}
else if ($color==azul){$textcolor = imagecolorallocate($imdef,0, 0, 255);}
else if ($color==negro){$textcolor = imagecolorallocate($imdef,0, 0, 0);}
else if ($color==blanco){$textcolor = imagecolorallocate($imdef,255, 255, 255);}
else if ($color==amarillo){$textcolor = imagecolorallocate($imdef,255, 255, 0);}
imagettftext($imdef, $font_size, $angle, $x_start, $y_start, $textcolor, $font_file, $texto);
ImageJpeg($imdef, "full/".$Ref.".jpg", 100);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you sure the original was of sufficient quality to be okay for this? Recompressing a JPEG will always lose more data.
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

The fact is that I'm adding some text (website logo) over images and I would like this text to look sharpen and ok but it looks a little blurry.
Post Reply