So i have a WordPress site with a bunch of animated GIFs and i manage to convert my images from GIF to JPG using this PHP script:
<?php
//This function gif2jpeg take three parameter as argument. two argument are optional
//first will take the gif file name. second for file name to save the converted file. third argument is an color array
//EXAMPLE:
$gifName = $_GET['gif_name']; //ABSOLUTE PATH OF THE IMAGE (according to its location)
$c['red']=255;
$c['green']=0;
$c['blue']=0;
echo gif2jpeg($gifName, '', $c);
function gif2jpeg($p_fl, $p_new_fl='', $bgcolor=false){
list($wd, $ht, $tp, $at)=getimagesize($p_fl);
$img_src=imagecreatefromgif($p_fl);
$img_dst=imagecreatetruecolor($wd,$ht);
$clr['red']=255;
$clr['green']=255;
$clr['blue']=255;
if(is_array($bgcolor)) $clr=$bgcolor;
$kek=imagecolorallocate($img_dst,
$clr['red'],$clr['green'],$clr['blue']);
imagefill($img_dst,0,0,$kek);
imagecopyresampled($img_dst, $img_src, 0, 0,
0, 0, $wd, $ht, $wd, $ht);
$draw=true;
if(strlen($p_new_fl)>0){
if($hnd=fopen($p_new_fl,'w')){
$draw=false;
fclose($hnd);
}
}
if(true==$draw){
header("Content-type: image/jpeg");
imagejpeg($img_dst);
}else imagejpeg($img_dst, $p_new_fl);
imagedestroy($img_dst);
imagedestroy($img_src);
}
?>
HOW TO USE?
Add the above code into a php file 'convertGifToJpeg.php'
Add an <img tag. <img src="http://mydomain.com/convertGifToJpeg.php?gif_name=/images/animated_image.gif" width="200" height="200" />
and its working like a charm!
Now I'm building a function to get an static image (JPG) from a GIF ( ANIMATED IMAGE ) and set it as the POST THUMBNAIL/FEATURE IMAGE every time a GIF is uploaded to a POST by doing this:
function gif_to_jpg($post_id) {
// if attachment is a gif extension
if($attachments[$i]['mime'] == 'image/gif' ){
$gifurl = wp_get_attachment_url( $post_id ); // GET THE URL OF THE MEDIA IMAGE UPLOADED
// build the string
$gif_to_jpg = 'http://mydomain.com/convertGifToJpeg.php?gif_name=' . $gifurl . '';
// next, download the URL of the JPG image
media_sideload_image($gif_to_jpg, $post_id, 'Sample GIF TO JPG image.');
// find the most recent attachment for the given post
$attachments = get_posts(
array(
'post_type' => 'attachment',
'numberposts' => 1,
'order' => 'ASC',
'post_parent' => $post_id
)
);
$attachment = $attachments[0];
// and set it as the post thumbnail
set_post_thumbnail( $post_id, $attachment->ID );} // end if
} // gif_to_jpg
add_action('save_post', 'gif_to_jpg');
But its not working at all... Can you guys help me out?
You can utilize a class for this called GifExtractor
This will extract frames from a gif file and allow you to do what you'd like with the resources (save them with imagejpeg or png with imagepng).
Looking at your code though, you have no definition of $i so it's essentially grabbing nothing from your post input and doing not able to really process anything here.
I would use wp_check_filetype() in this case.. It will return the extension and mime of your media in an array you define of your serverside gif file
var_dumping this data (if the image exists) should return an array with ['ext'] and ['mime'] containing the data needed for this portion.
For converting a gif to JPG we would first grab the frames (singular or multiple):
This will simply return the frames. A very simplistic function as well that can be easily expanded on with dimensions, etc.
We then grab the 1st frame and convert the resource inside our save_post hook:
Note: You can make sure your directory is even writable to begin with is_writable($directory_path) before you begin working with files / directories on the server side.
GifExtractor class allows many more possibilities such as getting durations of gifs, specifying time period of gif and what frames to get , etc ... I highly suggest looking into the code base on Github and familiarize yourself with the possibilities.
You can also build out your own custom jpg file location for when you do a imagejpg conversion.
Note: Keep in mind you will need to include the class libraries into your wordpress environment or utilize a composer environment and grab them from Packagist