How to convert gif to jpg

Posted on 2006-11-04
Last Modified: 2008-02-01

I upload gifs which I want to convert and save as jpgs. I do this:

$image = @imagecreatefromgif($filename);
  $image_p = imagecreatetruecolor($new_width, $new_height);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  imagejpeg($image_p, $filename_tn, 100); // 100% quality

Will that do the job, or is something else needed?

Question by:tr5
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 35

Accepted Solution

Raynard7 earned 500 total points
ID: 17875659
Hi,  that should be sufficient

the colour looks right, the conversion looks right and saving the jpeg looks right.

The only thing I would consider is if you are uploading these to be converted - why not convert the $_FILE to a jpeg on the fly rather than saving the gif to file and then re sampling it?

Author Comment

ID: 17875689
How would I do that?
LVL 35

Expert Comment

ID: 17875694
well - you have $image

= imagecreatefromgif($filename)

you should just be able to point it to the uploaded file

= imagecreatefromgif($_FILE['uploadedFileName']);

and the rest of the script would remain the same

Author Comment

ID: 17875710
ok. Thanks.

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question