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!
Convert image, with new filename and proper extension
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Convert image, with new filename and proper extension
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
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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Convert image, with new filename and proper extension
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 =]
-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.
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]);
-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.
Last edited by rdkill on 2009-03-26T14:46:28-07:00, edited 1 time in total.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert image, with new filename and proper extension
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