Page 1 of 1

trying to resize the same image to two different sizes

Posted: 2007-11-06T20:27:07-07:00
by chopthewood
The following php script resizes a loaded image to 450 pix max. It works great. But I also want it to resize it again to a smaller size (100) and save both sizes in different directories under the same file name. I use the same variables (except for the size and quality) and simply tack on another "exec" and "@copy" line to do the same procedure over again with the new size. The result is that I get one image in each of the different directories with the same name but they're also the same size (450). I tried 1000 different ways to do this but both images turn out to be 42kb in size and 450pix long.
Either one of the two sizes when done alone will work okay (I get the 450 and 100 sizes) but doing both together doesn't work. Any ideas? thanks

Code: Select all

$largeImgName=$_FILES['imgLarge']['name'];


// ------------------------ IMAGE MAGICK ---------------------------


// Temporary upload image name
$originalLarge_image = $_FILES['imgLarge']['tmp_name'];
// set the jpg quality
$theQualityLarge=75;
// Get the image dimensions
$sizeLarge=GetImageSize( $originalLarge_image );

// Maximum image width
$max_widthLarge = "450";

// Maximum image height
$max_heightLarge = "450";

// Resize the image and save
exec("convert -size {$sizeLarge[0]}x{$sizeLarge[1]}  $originalLarge_image -thumbnail $max_widthThumbx$max_widthThumb -quality $theQualityLarge $largeImgName");

@copy($largeImgName,"/home/jmthieme/public_html/flashRev7/testLarge/".$largeImgName) or die("Couldn't copy the Large image.");

echo "<img src=\"".$largeImgName."\">";

echo "File uploaded<br>";

Re: trying to resize the same image to two different sizes

Posted: 2007-11-07T10:32:31-07:00
by Bonzo
Thats strange I just do 2 exec operations on my image uploads and get 2 different size images.

I would do something like this:

Code: Select all

// Maximum image width thumb
$max_widthThumb = "150";

// Maximum image height thumb
$max_heightThumb = "150";

// Resize the image and save
exec("convert -size {$sizeLarge[0]}x{$sizeLarge[1]}  $originalLarge_image -thumbnail $max_widthThumbx$max_heightThumb -quality $theQualityLarge $thumbImgName");

// Maximum image width large
$max_widthLarge = "450";

// Maximum image height large
$max_heightLarge = "450";

// Resize the image and save
exec("convert -size {$sizeLarge[0]}x{$sizeLarge[1]}  $originalLarge_image -thumbnail $max_widthlargex$max_heightlarge -quality $theQualityLarge $largeImgName");

Re: trying to resize the same image to two different sizes

Posted: 2007-11-07T15:57:26-07:00
by chopthewood
Are you uploading two images or just one? I'm uploading just one and trying to save it as two different sized files in two different directories -- both files having the same file name . I tried it with the script below, but both images are sized according to the first one that is "exec-ed" In this case two files with the same name were saved in their own directories, but they were both sized to 100px max. ... no 450px for the large img.

Code: Select all

$largeImgName=$_FILES['imgLarge']['name'];
$thumbImgName=$_FILES['imgLarge']['name'];
//
// Temporary upload image name
$originalLarge_image = $_FILES['imgLarge']['tmp_name'];
$originalThumb_image=$_FILES['imgLarge']['tmp_name'];
//
// set the jpg quality
$theQualityLarge=75;
$theQualityThumb=60;
//
// Get the image dimensions
$sizeLarge=GetImageSize( $originalLarge_image );
$sizeThumb=GetImageSize( $originaLarge_image );
//

// Maximum image width thumb
$max_widthThumb = "100";

// Maximum image height thumb
$max_heightThumb = "100";

// Resize the image and save the thumb
// ------------------------ IMAGE MAGICK ---------------------------

exec("convert -size {$sizeLarge[0]}x{$sizeLarge[1]}  $originalLarge_image -thumbnail $max_widthThumbx$max_heightThumb -quality $theQualityLarge $thumbImgName");
@copy($thumbImgName,"/home/jmthieme/public_html/flashRev7/testThumb/".$thumbImgName) or die("Couldn't copy the Thumb image.");
// ---------------------------------------------
//
// Maximum image width large
$max_widthLarge = "450";

// Maximum image height large
$max_heightLarge = "450";

// Resize the image and save the large image
// ------------------------ IMAGE MAGICK ---------------------------
exec("convert -size {$sizeLarge[0]}x{$sizeLarge[1]}  $originalLarge_image -thumbnail $max_widthlargex$max_heightlarge -quality $theQualityLarge $largeImgName");

@copy($largeImgName,"/home/jmthieme/public_html/flashRev7/testLarge/".$largeImgName) or die("Couldn't copy the Large image.");

// ----------------------------------------------
//
echo "<img src=\"".$largeImgName."\">";
echo "<img src=\"".$thumbImgName."\">";
//

echo "File uploaded<br>";

Re: trying to resize the same image to two different sizes

Posted: 2007-11-07T16:23:00-07:00
by ZootSuitRyan
Clone is a good tool to save on lines of code and memory.

Code: Select all

convert image.tif -quality 85 ( +clone -write image_large.jpg +delete ) -resize 244x image_small.jpg

Re: trying to resize the same image to two different sizes

Posted: 2007-11-07T17:46:39-07:00
by chopthewood
I'm new at using IM and am not sure how I'd implement the +clone into my script so both images could be save to the proper directory. I've worked with the exec() thing (sorry, I'm a Mac person and this is a bit foreign) for a day or two but still have the problem I described as only 1 image size actually being written to the server. If I can get the script working correctly, that's about all I'll need from IM.

Re: trying to resize the same image to two different sizes

Posted: 2007-11-08T12:23:31-07:00
by Bonzo
I upload and watermark one image into two different sizes on my site but looking back at my code I am to embarrassed to post the code here - I wrote it when starting with ImageMagick and php !

Anyway ZootSuitRyan's piece of code works well:

Code: Select all

<?php
exec("convert House.jpg -quality 85 \( +clone -resize 450x -write image_large.jpg +delete \) -resize 150x image_small.jpg");
?>
As I understand it you are reading in your image, setting the quality, copying the image read in, resizing it, saving it to the directory and deleting the image you are editting it. You then go back to the original image, resize it and then save it.

Re: trying to resize the same image to two different sizes

Posted: 2007-11-09T13:23:11-07:00
by chopthewood
Still not working.. I tried using a modification of the suggested code to save the files in two separate directories but nothing is being copied. 'imgLarge' is the form variable for the uploaded .jpg image. There doesn't appear to be a sytax error so I'm sure it's just my own inexperience with IM.

Code: Select all

$largeImgName=$_FILES['imgLarge']['name'];
//

// ------------------------ IMAGE MAGICK ---------------------------



// Temporary upload image name
$originalLarge_image = $_FILES['imgLarge']['tmp_name'];
// set the jpg quality
$theQualityLarge=75;
$theQualityThumb=60;
// Maximum image width large
$max_widthLarge = "450";

// Maximum image height large
$max_heightLarge = "450";
// Maximum image width thumb
$max_widthThumb = "100";

// Maximum image height thumb
$max_heightThumb = "100";

// Resize the image and save
exec("convert $largeImgName -quality $theQualityThumb \( +clone -resize $max_widthThumbx -write /home/jmthieme/public_html/flashRev7/testThumb/$largeImgName +delete \)");
exec("convert $largeImgName -quality $theQualityLarge\( +clone -resize $max_widthLargex -write /home/jmthieme/public_html/flashRev7/testLarge/$largeImgName +delete \)");


//
echo "<img src=\"".$largeImgName."\">";
//

echo "File uploaded<br>";

Re: trying to resize the same image to two different sizes

Posted: 2007-11-09T13:32:58-07:00
by Bonzo
The code by ZootSuitRyan is good as its doing the two resizes with one line of code.

So your code wants to be:

Code: Select all

// Resize the image and save
exec("convert $largeImgName -quality $theQualityThumb \( +clone -resize $max_widthThumbx -write /home/jmthieme/public_html/flashRev7/testThumb/$largeImgName +delete \) -resize $max_widthLargex /home/jmthieme/public_html/flashRev7/testLarge/$largeImgName ");
You should be able to put the -quality $theQualityThumb inside the ( ). When you get the code to work you can try other improvements like that.

Basicly whatever is inside the ( ) is treated seperatly from the rest of the image manipulation.
As you may or may not know you need to escape the ( ) with a \ so it becomes \( \)

Re: trying to resize the same image to two different sizes

Posted: 2007-11-09T14:25:58-07:00
by chopthewood
Thanks Bonzo. I made the changes you mentioned but something's still missing somewhere. Nothing is being uploaded and the- echo "<img src=\"".$largeImgName."\">"; - is putting out a broken img. icon. Any other ideas?

Code: Select all

From the uploader_form:
<FORM METHOD="POST" ACTION="php/do_uploadIM2.php" ENCTYPE="multipart/form-data">
<p><strong>"Large" image to Upload:</strong><br>
<INPUT TYPE="file" NAME="imgLarge" SIZE="80"></P>
<P><INPUT TYPE="submit" NAME="submit" VALUE="Upload Files"></P>
</FORM>




----- THE PHP FILE

$largeImgName=$_FILES['imgLarge']['name'];
//

// ------------------------ IMAGE MAGICK ---------------------------



// Temporary upload image name
$originalLarge_image = $_FILES['imgLarge']['tmp_name'];
// set the jpg quality
$theQualityLarge=75;
$theQualityThumb=60;
// Maximum image width large
$max_widthLarge = "450";

// Maximum image height large
$max_heightLarge = "450";
// Maximum image width thumb
$max_widthThumb = "100";

// Maximum image height thumb
$max_heightThumb = "100";

// Resize the image and save
// Resize the image and save
exec("convert $largeImgName   \(-quality $theQualityThumb +clone -resize $max_widthThumbx -write /home/jmthieme/public_html/flashRev7/testThumb/$largeImgName +delete \) -resize $max_widthLargex /home/jmthieme/public_html/flashRev7/testLarge/$largeImgName ");

//
echo "<img src=\"".$largeImgName."\">";
//

echo "File uploaded<br>";

Re: trying to resize the same image to two different sizes

Posted: 2007-11-09T14:54:51-07:00
by Bonzo
After some messing about I got the code to work for me; thats one of the problems you have to get everything just right !

I have also added some error reporting which you can remove.

Problems ( From the error report you would not know this ! ):
1/ Needed a space here \( +clone
2/ Need to seperate the variable from the x; otherwise I assume it reads as $max_widthThumbx not $max_widthThumb x ( Putting a space will not work $max_widthThumb x )

Out of interest relative paths should work rather than absolute ones but again you can try that when you get the code to work.

Code: Select all

<?php

// Temporary upload image name
$originalLarge_image = "House.jpg";
// set the jpg quality
$theQualityLarge=75;
$theQualityThumb=60;
// Maximum image width large
$max_widthLarge = "450";

// Maximum image height large
$max_heightLarge = "450";
// Maximum image width thumb
$max_widthThumb = "100";

// Maximum image height thumb
$max_heightThumb = "100";

$largeImgName ="Large.jpg";
$smallImgName ="Small.jpg";
// Resize the image and save
// Resize the image and save
echo "<pre>";
exec("convert $originalLarge_image \( +clone -resize {$max_widthThumb}x -quality $theQualityThumb -write $smallImgName +delete \) -resize {$max_widthLarge}x $largeImgName  2>&1", $array); 
echo "<br>".print_r($array)."<br>"; 
echo "</pre>";


//
echo "<img src=\"".$largeImgName."\">";
echo "<img src=\"".$smallImgName."\">";
?>

Re: trying to resize the same image to two different sizes

Posted: 2007-11-09T16:56:56-07:00
by chopthewood
I'm having a tough time with this. This is the output for the code that follows it:
Array
(
[0] => sh: -c: line 0: syntax error near unexpected token `('
[1] => sh: -c: line 0: `/usr/bin/convert-orig /tmp/phpEuZTU6 ( +clone -resize 100x -quality 60 -write SoulMates.jpg +delete ) -resize 450x SoulMates.jpg'
)

1

//-------------------------------------

I don't understand how the second file gets written if there's no "-write" Only the first image has a -write, the second is just -resize. If there's no pathname, where does it write to? The root folder or the folder with the php script? I can't find the above syntax error.
I'd like to dind a good source for understanding the IM exec syntax in detail, with good examples.Please let me know if you know of any.
thanks


Code: Select all

$originalLarge_image = $_FILES[imgLarge][tmp_name];
// set the jpg quality
$theQualityLarge=75;
$theQualityThumb=60;
// Maximum image width large
$max_widthLarge = "450";

// Maximum image height large
$max_heightLarge = "450";
// Maximum image width thumb
$max_widthThumb = "100";

// Maximum image height thumb
$max_heightThumb = "100";

//$largeImgName ="Large.jpg";
//$smallImgName ="Small.jpg";
$largeImgName=$_FILES[imgLarge][name];
$smallImgName=$_FILES[imgLarge][name];
// Resize the image and save
// Resize the image and save
echo "<pre>";
exec("convert $originalLarge_image \( +clone -resize {$max_widthThumb}x -quality $theQualityThumb -write $smallImgName +delete \) -resize {$max_widthLarge}x $largeImgName  2>&1", $array);
echo "<br>".print_r($array)."<br>";
echo "</pre>";


//
echo "<img src=\"".$largeImgName."\">";
echo "<img src=\"".$smallImgName."\">";


Re: trying to resize the same image to two different sizes

Posted: 2007-11-10T05:14:24-07:00
by Bonzo
Sorry I am getting to carried away here lets go back to basics:

Code: Select all

<?php
// This is the basics to resize to a thumb 150 and a larger image 450
//exec("convert input.jpg -resize 150x thumb.jpg");
//exec("convert input.jpg -resize 450x large.jpg");

// Using the variables for the information 
$input = "input.jpg";
$thumb = "thumb.jpg";
$large = "large.jpg";
$thumb_width = "150";
$large_width = "450";

exec("convert $input -resize {$thumb_width}x $thumb");
exec("convert $input -resize {$large_width}x $large");

echo $thumb;
echo $large; 
?>
I do not know what is causing the errors you have.

You could try looking at this page for more info on how IM works http://www.imagemagick.org/script/comma ... essing.php

Re: trying to resize the same image to two different sizes

Posted: 2007-11-10T07:26:09-07:00
by chopthewood
First, I want to thank you for all your help. Even though the problem is not solved yet, I have learned a great deal from the process. I liked the error feedback that you used in the last example. Okay, back to the basics:
I am bumping into the original problem which, in much simpler form is this:
Note that I am NOT using two different images as you show in your example. I only have one which is from an Input form:

$originalLarge_image = $_FILES[imgLarge][tmp_name];

So I take this original input as the source to "-convert". I do the functions that I need to do on it for each of the two images that will result:
$smallImgName (this will be 100x100)
and $largeImgName (this will be 450x450)
I use "-thumbnail" instead of "-resize" because I want to eliminate the .jpg profile.

Code: Select all

exec("convert $originalLarge_image -thumbnail {$max_widthThumb}x{$max_heightThumb} $smallImgName");
exec("convert $originalLarge_image -thumbnail {$max_widthLarge}x{$max_heightLarge} $largeImgName");

The thing I can't figure out is why this results in two images that are 450 x 450!
echo "<img src=\"".$largeImgName."\">";// this is 450 x 450
echo "<img src=\"".$smallImgName."\">";// this is 450 x 450 as well

Yet I have set this for the thumb image:
$max_widthThumb=100
$max_heightThumb=100

Re: trying to resize the same image to two different sizes

Posted: 2007-11-10T10:16:36-07:00
by chopthewood
EUREKA
It finally works!
I went back to what I was doing a couple days ago and (with Bonzo's previous help) I noticed the lack of {} in {$max_widthLarge}x{$max_heightLarge}. For some reason, however, "$max_widthLargex$max_heightLarge" actually was working in the 1st "exec" and saving the image to the server in the 1st "@copy". But on the second resized image, instead of resaving it as a smaller image, it saved it as the same Large size.

The bottom line is WHY ON EARTH would it work AT ALL without the {} around {$max_widthLarge}x{$max_heightLarge} as below:

exec("convert -size {$sizeLarge[0]}x{$sizeLarge[1]} $originalLarge_image -thumbnail $max_widthLargex$max_heightLarge -quality $theQualityLarge $largeImgName");

But it does work. Hmmm. Is it fair to say that IM is a bit sloppy in this respect?
----------------------------------

Code: Select all

exec("convert -size {$sizeLarge[0]}x{$sizeLarge[1]}  $originalLarge_image -thumbnail {$max_widthLarge}x{$max_heightLarge} -quality $theQualityLarge $largeImgName");
@copy($largeImgName,"/home/jmthieme/public_html/flashRev7/testLarge/".$largeImgName) or die("Couldn't copy the Large image.");
exec("convert -size {$sizeLarge[0]}x{$sizeLarge[1]}  $originalLarge_image -thumbnail {$max_widthThumb}x{$max_heightThumb} -quality $theQualityLarge $largeImgName");
@copy($largeImgName,"/home/jmthieme/public_html/flashRev7/testThumb/".$largeImgName) or die("Couldn't copy the Large image.");



My thanks to ZootSuitRyan for the original suggestion and to Bonzo from hanging in there and doing homework for me.

Re: trying to resize the same image to two different sizes

Posted: 2007-11-11T09:34:56-07:00
by Bonzo
I am glad you got sorted in the end chopthewood.