Page 1 of 2
Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T12:50:55-07:00
by raindance
Hi
I am a total newbie to ImageMagick and would appreciate some direction. My developer is unfamiliar with it and here's what we're trying to do:
We allow a user to upload a batch of photos at one time. But they upload massive high res files and I want them to be 72dpi and max width 300px, max height 300px.
I've installed ImageMagick and MagickWand (not positive I did MagickWand correctly). Can someone please point us in the right direction or offer sample code and steps?
Thanks so much in advance
Laura
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T13:20:37-07:00
by Bonzo
You can not modify a photo until it is on the server all you can do is prevent them uploading large files in the first place.
Here is some old upload code of mine you can use and there are other examples of how to use php and Imagemagick on my site.
Make sure you validate any user input; the file should be OK as if it is not an image file the code will fail.
Code: Select all
<?php
// If the form has been submitted do this
if ( $Submit ) {
// Temporary upload image name
$original_image = $_FILES['filename']['tmp_name'];
// Get the image dimensions
$size=GetImageSize( $original_image );
// Name to save the image as - in this case the same as the original
$new_image = $_FILES['filename']['name'];
// Maximum image width
$max_width = "200";
// Maximum image height
$max_height = "90";
// Resize the image and save
exec("convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height $new_image");
echo "File uploaded<br>";
echo "<img src=\"".$new_image."\">";
}
else { ?>
<p>File to upload:</p>
<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="Submit" name="Submit" value="Submit" />
</form>
<?php } ?>
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T13:37:25-07:00
by raindance
Thanks so much for the code, I'll pass it along.
One question...the thing is that I'm trying to allow them to upload whatever they want because the users won't know how to make them lower resolution, and even if they do, I'm trying to make it wasy for them to grab a batch of photos and upload them.
It looks like your code might handle that anywaay though if it's creating a temp file?
Sorry, I'm trying to get a handle on how this works. I don't want to require them to resize the resolution first, I want our system to do it for them on upload. It's fine if big files get uploaded to the server, and resized there, then the big original one can be dumped. It looks like the -resample command might do it? I want it to takes what the user uploads and drop the resolution down to 72 rather than wasting server space on 5mb photo files which are only 300x300 and only ever intended for web viewing.
Thanks
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T14:03:33-07:00
by Bonzo
When a file is uploaded it is placed in the temp folder and given a tempory name; the resize code loads the tempory file into memory modifys it and then saves it, the tempory image will get deleted at some point automaticaly by php - not sure when or how !
One question...the thing is that I'm trying to allow them to upload whatever they want because the users won't know how to make them lower resolution, and even if they do, I'm trying to make it wasy for them to grab a batch of photos and upload them.
I know the feeling as I have the same problem on a forum - al lot of people seem "computer challanged" but not every one is interested in computers. One of the worst offenders is my wife
I think the default setting is 72dpi but if not you can add -density I belive. If you use -thumbnail it will strip out all EXIF data and depending on your version it may or may not remove any colour profiles.
Get your upload form working and you can "tweek" it.
This code is watermarking multiple images on upload but can be modifed to resize instead:
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
}
?>
You will have to change this line: "if ( $Submit ) {" to something like "if (!empty($_POST['Submit'])) {"
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T14:19:46-07:00
by fmw42
Please note that density only has to do with printing. It does no matter if the image is 300 dpi or 72 dpi as far as the file size goes. The important thing is the number of pixels in the image and bits/pixel and compression. The density only controls how big the image will print on paper. Though it does not hurt for computer display to make the density 72. The way you resize the image will produce different quality results. You need to decide what quality you want (via -resize or -scale or -sample) and then what format you want to use to save the result (jpg will have some compression included that will affect quality) vs png or tiff or bmp. But for all these formats you can have 16-bits per channel, 8-bits per channel (rgb) or 8 bits per image (pseudocolor) depending upon the -depth and -type you select for output.
Filesize may also depend upon ancillary data stored with the image, such as profiles and EXIF data. So you may want to use -strip to remove that as well if this is critical.
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T14:56:17-07:00
by raindance
Thank you both for your help!
The photos are never going to need to be bigger than 300 x 300 and will never be printed. That being said, then which of the options you have both mentioned would make the most sense for quality/file size?
I'm just trying to make it (1) easy for them and (2) not use up space needlessly on my server.
THANK YOU BOTH!
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T14:58:37-07:00
by raindance
Anything which would be helpful beyond that to pass along to my dev who has not worked with this before? Any overview/details which would help him implement efficiently?
By the way Bonzo, I think most users uploading fall into your wife's category...image resizing is not so obvious to anyone it seems. I can see why...it totally makes no sense if you are not somewhat computer literate I think.
Thanks

Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T15:07:04-07:00
by fmw42
It really then depends upon the quality you want vs file size. -resize produces the best results, -scale and -sample are faster but the quality won't be so good. Then the file size will depend upon whether you produce 16bit per channel, 8-bit per channel or 8-bit or less per image. Quality will go down but so will filesize. If you want to keep good quality and reduce filesize, then create 8-bit per channel images in jpg format with a reasonable compression.
convert -define jpeg:size=300x300 yourimage -resize 300x300 -depth 8 -quality XX -density 72 -units pixelsperinch resultimage
where XX is the jpeg quality, nominally 92 but you don't need that much. Perhaps 75 would be adequate. You will need to test on you own what quality vs filesize you want.
See
http://www.imagemagick.org/Usage/resize/
http://www.imagemagick.org/Usage/formats/#jpg_read
http://www.imagemagick.org/script/comma ... php#resize
http://www.imagemagick.org/script/comma ... p#geometry
http://www.imagemagick.org/script/comma ... hp#quality
http://www.imagemagick.org/script/comma ... hp#density
http://www.imagemagick.org/script/comma ... .php#units
http://www.imagemagick.org/script/comma ... .php#scale (will be faster but quality will be less)
http://www.imagemagick.org/script/comma ... php#sample (will be faster but quality will be much less with aliasing)
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T15:14:11-07:00
by raindance
Got it thanks so much.
And thanks for the watermarking also, I want to use that also for my own.
Thanks!
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T16:36:18-07:00
by raindance
So this is what my developer did and says he can't get it to work...I sent him the link to this posting. Is he on the right track? He says it is not resizing and is asking if there are any error logs specific to ImageMagick?
Any help appreciated....
for ($mx=0;$mx<count($_FILES["pict"]["name"]);$mx++)
{
if($_FILES["pict"]["name"][$mx]!="")
{
$capt = $_POST['image_caption'][$mx];
$tmp_name = $_FILES["pict"]["tmp_name"][$mx];
if(!is_dir(IMAGE_UPLOAD_PATH .'images/manual_images/'.$folderName))
{
mkdir(IMAGE_UPLOAD_PATH.'images/manual_images/'.$folderName.'/',0777);
chmod(IMAGE_UPLOAD_PATH.'images/manual_images/'.$folderName.'/',0777);
mkdir(IMAGE_UPLOAD_PATH.'images/manual_images/'.$folderName.'/thumb/',0777);
chmod(IMAGE_UPLOAD_PATH.'images/manual_images/'.$folderName.'/thumb/',0777);
}
$filename = strtolower($_FILES["pict"]["name"][$mx]) ;
$exts = explode(".",$filename);
$n = count($exts)-1;
$exts = $exts[$n];
if($exts=='jpg' || $exts=='jpeg')
{
$temp_file_path=IMAGE_UPLOAD_PATH .'images/manual_images/'.$row_org['country'].'_'.$row_org['name'].'_'. rand(100,99999).'.'.$exts;
$ran= 0;
do
{
$ran++;
$bc_image_path_up = IMAGE_UPLOAD_PATH .'images/manual_images/'.$folderName . '/'.$row_org['country'].'_'.$row_org['name'].'_'.$ran.'.'.$exts;
$bc_image_path_thumb_up = IMAGE_UPLOAD_PATH .'images/manual_images/'.$folderName . '/thumb/'.$row_org['country'].'_'.$row_org['name'].'_'.$ran.'.'.$exts;
$bc_image_path = ABSOLUTE_PATH .'images/manual_images/'.$folderName . '/'.$row_org['country'].'_'.$row_org['name'].'_'.$ran.'.'.$exts;
$bc_image_path_thumb = ABSOLUTE_PATH .'images/manual_images/'.$folderName . '/thumb/'.$row_org['country'].'_'.$row_org['name'].'_'.$ran.'.'.$exts;
}while(file_exists($bc_image_path_up));
move_uploaded_file($tmp_name, $temp_file_path);
$Details = getimagesize($temp_file_path);
//var_dump($Details);
if($Details[0]>325)
{
@$objResize = new RVJ_ImageResize( $temp_file_path, $bc_image_path_up, 'W', 325);
}
else
{
@$objResize = new RVJ_ImageResize( $temp_file_path, $bc_image_path_up, 'W', $Details[0]);
}
@$objResize = new RVJ_ImageResize( $temp_file_path, $bc_image_path_thumb_up, 'C', array('70', '70'));
unlink($temp_file_path);
$sql = "insert into hotel_images (hotel_id,image_path,image_thumbnail_path,image_caption,manual) values ('" . $org_id . "','" . $bc_image_path . "','" . $bc_image_path_thumb. "','" . $capt . "',1)";
$res = mysql_query($sql);
if ($res)
{
$i++;
$sucMessage = $i." Images uploaded successfully...Note: if any images are missing, please check file type. Images must be .jpg only. Thanks.";
}
}
}
}
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T16:37:55-07:00
by raindance
He says that the command is not executing. THe folder is changed to writeable re permissions....any obvious suggestions?
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T17:39:05-07:00
by fmw42
I have no idea what RVJ_ImageResize is? Sorry, I don't program any IM APIs, only the command line. So I cannot help with your code.
Looks like a mix of PHP and MagicWand? If so, why not just use PHP and run the exec function to do the resize?
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-24T18:51:09-07:00
by raindance
No idea...I'm way over my head now

Will pass along, thanks.
Not sure why he didn't just use your script in the first place..sure looked on target to me...learning curve maybe.
Thanks so much, I really appreciate your help!
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-25T13:17:51-07:00
by Bonzo
I always say get the simple part working and then build in all the bells and whistles.
Is Imagemagick actualy working ?
An error reporting method:
Code: Select all
<?php
$array=array();
echo "<pre>";
exec("convert House.jpg -channel B -separate Output_test.png 2>&1", $array);
print_r($array);
echo "</pre>";
?>
I just use php with the IM command line as its simpler and there are more options.
Re: Best way to reduce file size/resolution on upload?
Posted: 2010-10-25T13:26:37-07:00
by raindance
Hi Bonzo
I asked the hosting company to confirm it was installed properly and they said yes. Beyond that...we didnt know how to make sure it was. Dev was asking where to error logs for im specifically but i dont think there is such a thing probably, is there? Thanks for your error reporting code...we'll try that...