Page 1 of 1
Convert image, with new filename and proper extension
Posted: 2009-03-24T16:48:15-07:00
by rdkill
Hi,
I'll be using convert using the shell from PHP and I need to process temporary uploaded files into their respective public directories. I'd like to take advantage of IM's ability to read image formats and append the correct file extension to the newly converted images. The images could be of any type, and the source files might not have the correct (if any) extension.
Basically, I need to convert <random filename> into <image_id>.<image_file_ext>. What's the fastest way to do this, with the least amount of individual commands?
Thanks!
Re: Convert image, with new filename and proper extension
Posted: 2009-03-24T23:56:40-07:00
by anthony
I do not think IM will add the right extension to a file name unless
morgify -format is being used, or some type of user specified output filename escape is used.
See
http://www.imagemagick.org/Usage/files/#save_escapes
However I myself wrote a shell script to 'fix' the suffix of files for at lease soem file formats. This also uses 'file magic' just not 'image magick' see the man page for the command "file"
download this perl script from...
http://www.cit.gu.edu.au/~anthony/softw ... _suffix.pl
WARNING: you should replace the "mv" command with a safer version. In my own case I use a command I created called "merge" that appends a number
if the move was going to overwrite a file in the destination directory.
see
http://www.cit.gu.edu.au/~anthony/software/merge.sh
Re: Convert image, with new filename and proper extension
Posted: 2009-03-26T14:43:22-07:00
by rdkill
Thanks for the help, the -format modifier sent me in the right direction. I decided to use
identify with
convert rather than try to hack everything into one command. Here's my PHP code, if anyone is interested. It seems to be working fine. Feel free to point out any possible optimizations =]
Code: Select all
$type = null;
$w = 0;
$h = 0;
$output = exec('identify -format "%m %w %h\\\n" '.$tempfile);
$image_data = explode("\n", $output);
foreach($image_data as $line)
{
$data = explode(' ', $line);
$type = $data[0];
$w = max($w, $data[1]);
$h = max($h, $data[2]);
}
$ext = array('GIF' => '.gif', 'JPEG' => '.jpg', 'PNG' => '.png');
// Create a small thumb, 125x100, cropped-to-fit
exec('convert -size 300x300 '.$tempfile.' -thumbnail "125x100^" -gravity center -extent 125x100 images\\th_'.$filename.$ext[$type]);
//Create a medium thumb, max size of 300x600.
exec('convert -size 600x1200 '.$tempfile.' -thumbnail "300x600>" images\\md_'.$filename.$ext[$type]);
//Create a full-size image, just keep it within 800x3200px.
exec('convert '.$tempfile.' -resize "800x3200>" images\\lg_'.$filename.$ext[$type]);
-It only checks 3 image formats so far. In the event that someone uploaded an obscure image format, would it be a good idea to default the output format to JPEG?
-I haven't been able to locate any info regarding
convert's output. It seems like if everything works fine, exec() returns an empty string. However, I'd like to be able to catch any possible error in PHP and handle it.
Re: Convert image, with new filename and proper extension
Posted: 2009-03-26T14:46:22-07:00
by fmw42
You may be able to speed it up a bit by adding -ping to your identify command. See
http://www.imagemagick.org/Usage/basics/#controls