Automated Image Resizing

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
sotomayor

Automated Image Resizing

Post by sotomayor »

I have a script for a social network which limits photo upload to whatever specific size I set in admin; e.g. If I set the max photo size to 200kb (to save bandwith and storage space) but the member's photo exceeds that size, they will not be able to upload the photo, they will get an error stating photo exceeds allowed size. I need a solution which may allow most photo sizes to be uploaded but will automatically resize them before storing them on server and posting on site. On facebook I could upload a 5Mb photo, but they have a solution which automatically resizes the image to 200kb. I was recommended to this forum by a company which has the solution I need for windows only (http://tangotools.com/jpegsizer/). Running a windows box on the network to make it work was not an option due to the extra expense it would create. I admit to be a newbie and may have missed it after going through the forum, but I could not find a specific reference to the features I seek. I would apreciate any guidance from you all.( Some of the hosting server's features are ,PHP-5 / MySQL-5 / Apache-2, RED5 / RMS Modules, FFMPEG Modules). I also want to Thank everyone currently involved, and past contributers, of these magnificient tools. Thank you all for the time and efforts, for any assistance you may be able to provide.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Automated Image Resizing

Post by fmw42 »

see -resize and its arguments at

http://www.imagemagick.org/script/comma ... php#resize
http://www.imagemagick.org/script/comma ... p#geometry
http://www.imagemagick.org/Usage/resize/

and also reading in jpegs at:

http://www.imagemagick.org/Usage/formats/#jpg_read


and command line syntax at:

http://www.imagemagick.org/Usage/basics/#why


For PHP examples, see http://www.rubblewebs.co.uk/index.php


If you can explain in more detail exactly what you want to accomplish, others can suggest commands sequences to use. IM does not have a unique, simple option to resize to a given filesize only to given dimensions. But you can set compression quality levels for some image types, however, that does not guarantee a given filesize. However, one can script something that makes a good guess at the dimensions from the original image's dimensions and its filesize. Then it could be iterated if needed to get to your specified size.
sotomayor

Re: Automated Image Resizing

Post by sotomayor »

Thank you for such promt assistance. I will attempt to be more presice as to what it is I need. I need a code that will take images during upload into members profiles (jpg, jpeg, gif and png) and automatically resize/ compress them. The script I have allows me to set the size limits for photo upload. I need a code that will automatically resize/compress the photo to a smaller size during upload and before displaying said image. I would want it to allow multiple upload, although it will mostlikely resize/ compress the images one by one individually. This will prevent me from setting a photo size limit for upload, allowing people to upload their photos without problems. Retaining picture quality it's a most, although I noticed that it's ImageMagick's priority. Thanks again for the help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Automated Image Resizing

Post by fmw42 »

Please clarify what you mean by "The script I have allows me to set the size limits for photo upload. I need a code that will automatically resize/compress the photo to a smaller size during upload and before displaying said image"

Are you saying that you want the size smaller than your upload limit or you want any size to be uploaded but then resized to your upload limit? What tolerances on size do you want? By size do you mean file size or image dimensions? Give me a description of one example of what you want.
sotomayor

Re: Automated Image Resizing

Post by sotomayor »

Thank you, I believe you have guided me to a better definition. Yes, I want anysize to be uploaded and then resized to my upload limit. My upload limit would be 200kb, (I hope this was what you meant by tolerance). And yes I meant file size. Thank you. As an example I could only give you facebook. I know although you're allowed to upload up to 5Mb size photo, the photo's are automatically resized to 200kb. This exact features would be what I look for. Thank you.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Automated Image Resizing

Post by Bonzo »

You can not resize by kb size and why would you - your photos would be different sizes depending on original quality.
I know although you're allowed to upload up to 5Mb size photo, the photo's are automatically resized to 200kb.
This is probably limited by the server and unless your php.ini file has been modified your maximium file upload size is probably 2Mb; uploading large files is a waste of bandwidth and server process time. Its much quicker done on the PC but I know some users are not computer literate enough to do it and I would have a link to some instructions to reduce the filesize to something a bit closer to 200kb.

You could modify this code - although on file sizes to large it will just fail to upload rather than give an error. Even when outputting an error on attempts to upload large files some users just do not understand.
http://www.rubblewebs.co.uk/imagemagick ... k_form.php

Code: Select all

<?php
if ( $Submit ) {

$up_files = $_FILES['filename']; 

$counter = 1; 
    
while($counter <= count($up_files))  { 

$img_name = $up_files['name'][$counter]; 

$tempory_location = $up_files['tmp_name'][$counter]; 

$new_name = "Image_name_".$counter.".jpg";  
        
move_uploaded_file( $img_name, $tempory_location ); 

exec("convert $tempory_location -gravity North -font Helvetica-Bold -pointsize 16 -draw \" fill black text 0,0 'Images copyright of Anthony'\" -draw \" fill white text 1,1 'Images copyright of Anthony'\" $new_name" );

        $counter++; 
    }   
}

else {
?>
<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
<label>Images to upload<br>
    Image 1 : <input name="filename[1]" type="file"><br>
    Image 2 : <input name="filename[2]" type="file"><br>
    Image 3 : <input name="filename[3]" type="file"><br>
    Image 4 : <input name="filename[4]" type="file"><br>
</label>
<input type="Submit" name="Submit" value="Submit" />
</form>
<?php
}
?> 
If the filesize is to large you could include this code to display an error although it will need modifying to suit the upload code used:

Code: Select all

if( ( $_FILES[photos][error][$i] == 1 ) or ( $_FILES[photos][error][$i] == 2 ) ){
echo "<p class=\"note\">Photo ".( $i + 1 ).". was too large and not uploaded.</p>";
} 
This will still try and upload the file and will only display the error when the setting in php.ini file is reached; you may be able to do a check with JavaScript.
You can set a maximium filesize in your form as well but I think it is not totaly reliable.
sotomayor

Re: Automated Image Resizing

Post by sotomayor »

Thank you for your wisdom. I mentioned 5Mb as an example based on the results of testings I did uploading photos to facebook. I agree 2Mb to be the norm on max file size per photo allowed on social networks. I will follow your instructions and provide an update as soon as it's done. My internet provider it's currently down and I'm schedule to go on vacation in 34 hrs. All these means thre's a possibility I wont be able to do anything, which makes me an ansious man, I can't wait. Thank you, I apologize for the delay in answering your post. I'm currently at the public library using their WiFi :D Thanks again for your time and wisdom.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Automated Image Resizing

Post by fmw42 »

My question about tolerance is how close to 200 Kb do you need to get. If I wrote a script, I would simply convert the image to same image type as the input (as IM may produce a different file size for your jpg than your original). Then get the converted filesize. Then compute the ratio of 200 Kb/converted filesize and convert to percent. Then use -resize to scale the image dimensions down by that percent, keeping the aspect ratio. This would then produce an image of nearly 200 Kb (but may not match it exactly), but the image dimensions would be smaller. That is the only way I can think of to consistently (for any image type) keep quality up and not play with compression that is not available for every image type and end up with something near your 200 Kb. I will try to generate that script. Is this what you had in mind in terms of how to reduce the filesize (namely, by reducing the image dimensions)?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Automated Image Resizing

Post by fmw42 »

I have just created and uploaded a new bash/unix script, downsize, to reduce the dimensions of an image such that it achieves a desired file size.
Post Reply