Page 1 of 1
Problem resizing pictures !!
Posted: Fri Feb 11, 2005 2:28 pm
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);
Posted: Fri Feb 11, 2005 2:31 pm
by feyd
the code posted does not do resizing.
imagecreatetruecolor() > imagecopyresampled() > imagejpeg(.., quality)
Posted: Fri Feb 11, 2005 2:45 pm
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!
Posted: Sat Feb 12, 2005 12:47 pm
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?
Posted: Sat Feb 12, 2005 1:12 pm
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.
Posted: Sat Feb 12, 2005 1:21 pm
by feyd
isn't that what I said in my post? yes.
Posted: Sat Feb 12, 2005 2:32 pm
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...
Posted: Sat Feb 12, 2005 2:38 pm
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
Posted: Sat Feb 12, 2005 3:24 pm
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);
Posted: Sat Feb 12, 2005 3:39 pm
by feyd
you sure the original was of sufficient quality to be okay for this? Recompressing a JPEG will always lose more data.
Posted: Sat Feb 12, 2005 4:17 pm
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.