jpeg to gif conversion going wrong

images php coding issues or problems here.

Moderators: macek, egami, gesf

bobthebuilder
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Feb 26, 2011 11:35 am

jpeg to gif conversion going wrong

Postby bobthebuilder » Sat Feb 26, 2011 12:15 pm

Hello everyone. I have used the following simple code to load a jpeg file and save it as a gif file:

Code: Select all

$in_img = imagecreatefromjpeg("test_img.JPG");
    imagejpeg( $in_img, "out.jpg", 100);
    imagegif( $in_img, "out.gif"); 


whilst out.jpg looks great

http://i586.photobucket.com/albums/ss308/galoisfield/out-1.jpg

out.gif turns my daughter's face yellow!

http://i586.photobucket.com/albums/ss308/galoisfield/out.gif

Anyone know why this is or how to overcome it?

Thanks in advance.

Raint
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Mar 08, 2011 10:16 pm

Re: jpeg to gif conversion going wrong

Postby Raint » Wed Mar 09, 2011 2:56 am

gif is format with only 256 colors, so photo quality will allways decrease alot...



___________________________________________________________________________________
Try your PHP code instantly. http://www.freephptest.com

phmyway
New php-forum User
New php-forum User
Posts: 6
Joined: Mon Feb 28, 2011 11:45 pm

Re: jpeg to gif conversion going wrong

Postby phmyway » Thu Mar 10, 2011 12:45 am

I think I agree with Raint that the quality of gif is lower that jpeg. But you try the following coding if it works
<?php
//read all the attributes of the image first with this:
list($width, $height, $type, $attr) = getimagesize($ImageName);

//then
if ($type == 2) { //this is to mean if the format type is JPG
rename($ImageName, $newfilename);
} else {
if ($type == 1) { //that is to mean if the format type is GIF
$image_old = imagecreatefromgif($ImageName); //that is change GIF or gif formats to jpg
} elseif ($type == 3) {
$image_old = imagecreatefrompng($ImageName); //that is change PNG or png formats to jpg
}

//convert the image to gif
$image_gif = imagecreatetruecolor($width, $height);
imagecopyresampled($image_gif, $image_old, 0, 0, 0, 0, $width, $height, $width, $height);
imagegif($image_gif, $newfilename);
imagedestroy($image_old);
imagedestroy($image_gif);

}

//bobthebuilder: I hope the above function may keep the resolution intact.
//Bye for now and pls let me hear from you.
?>


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest