Forum Moderators: coopster

Message Too Old, No Replies

Optimize pictures on unix

         

DenRomano

6:48 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



Hi,

I currently have to deal with all the uploading of product pictures myself. I would like to add to my back end PHP program the functions to allow my employees to do it.

So I will need to be able to have a program that does several processes that I currently do manually. I need a program that will run on my unix server that I can call by command line and it will

1. Resize the picture. I need to 2 copies. One that is max 350 x 350 and a thumbnail that is 100 x 100. Keeping tha aspect ratio correct

2. Optimize the graphic for fast loading

Does anyone know of a program like this?

Nutter

9:39 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



ImageMagick or GD would be my first thoughts. I've never used GD personally, but I know ImageMagick can do what you're asking. ImageMagick is a series of command line programs. I've used it for a script that makes thumbnails with a max dimension of 100 and one of 500. You just punch in max dimension and neither height nor width will be above that. The smaller will scale to match.

- Ryan

HughMungus

11:25 pm on Feb 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a website where I occassionally post pictures and I wanted what you want which is to be able to just upload the image and have it resized and thumbnailed. What I ended up with is an upload page that has a series of scripts that let me specify a file from my hard drive to be uploaded, have the image resized, and have a thumbnail created.

Not sure if you need this part, but, for the uploading on the input form page, I have this:

<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input name="uploadimage" type="file" />

Which has something like this on the "page" that processes that form input:

if ($uploadimage<>"")
{
$uploaddir = '/your/picture/diretory/';
$uploadfile = $uploaddir . basename($_FILES['uploadimage']['name']);
move_uploaded_file($_FILES['uploadimage']['tmp_name'], $uploadfile);
$uploadedimagename = basename($uploadfile);
$image = $uploadedimagename;
}

So then you have an image on your server to work with.

This function will create a thumbnail and it can be modified to resize an image, also (just change the dimensions).

function createthumb($name,$filename,$new_w,$new_h){
global $gd2;
$system=explode(".",$name);
if (preg_match("/jpg¦jpeg/",$system[1])){
$src_img=imagecreatefromjpeg($name);
}
if (preg_match("/png/",$system[1])){
$src_img=imagecreatefrompng($name);
}

I'd just post a link to the tutorial but I think that's frowned upon here. In the meantime, look at the manual for "imagecreatefromjpeg".

DenRomano

12:18 am on Feb 22, 2005 (gmt 0)

10+ Year Member



I looked up both GD and imageMagick and both will resize but I can not find where they say they will optimize the image (reduce the file size for faster downloading).

Did I miss it?

Nutter

2:50 am on Feb 22, 2005 (gmt 0)

10+ Year Member



I don't know about optimizing, but I did find one thing important while doing this. When I first ran my script the large and small files were almost the same size. Both still really small, but too close to the same size to worry about having two copies. Turns out the ICC profile takes up a bunch of space, relative to the fairly small jpegs you're probably going to be using.