PDA

View Full Version : Proportionate Images


Corey Bryant
December 23rd, 2004, 19:41
We are helping a client set up a new shopping cart on his website. We told him - send all images 640X480 over to us (or 320X240, etc). She was taking the pictures & sending them over. Do I even bother to really look at the images? (No).

So I upload them all for her & we start to look at the multi-view. They are all distorted. I look at the images, they are 181X603, etc. Weird sizes. My business partner emails her (since this is his client), and tells her to takes photos that are 640X480. She tells him she is.

After 15 minutes of explaining this to my business partner that clearly she is doing something very bad to these images, I email her & tell her to take me thru the steps.
First I upload direct from the camera, then edit in Coral draw program. The editing done is usually cropping only. I can shoot the shoes in higher resolution if needed.
The best thing though, I have been a victim of scope creep in the past & now it is happening to him. They just added about 20 more shoes & 4 more categories.

sonicgroup
December 23rd, 2004, 20:11
I assume, given your past posts, that this cart is in ASP? I don't know much about ASP, but I do understand there are image manipulation libraries for it similar to GD for PHP. It shouldn't be difficult to resize the images proportionate to the original image size (by specifying a max width/height and adjusting the image in either direction).

I did this for my Snaps! Gallery software. If you want, I can post the PHP code and you might be able to translate that to something usable in ASP.

Corey Bryant
December 23rd, 2004, 21:10
Yes it is. We have talked about this actually to do something like that. We have been contemplating it for some time - but been working on adding features (like multiview, and a few others).

But yes - that would be cool. I can get that to them & see what they can do with it. I mean I have about 5 different imaging components installed so we should be able to do something.

sonicgroup
December 23rd, 2004, 21:30
Ok, here you go - it's commented, but I'll throw in some additional stuff. It's also built for handling multiple image types (since they use different functions), so that may be something you can throw out altogether (the switch statement).

[syntax:5a1f6f0dfc="php"]
<?php
// The file
$filename = $_GET['image'];
$size = $_GET['size'];
// Set a maximum height and width - this is passed through the URL that calls the script
$width = $size;
$height = $size;
// Content type - the script outputs all results as JPGs
header('Content-type: image/jpeg');

// Get new dimensions and mimetype
// OK, here is where the nitty gritty starts
// this gets the image size (width and height) of the original image
list($width_orig, $height_orig, $mimetype) = getimagesize($filename);

// if the original width is smaller than our max width we set at the beginning,
// we set the width and height to the original image's dimensions
// I use width here because I'm not worried about the height too much
// If you are more worried about it being too tall, change the comparison to
// if ($height_orig < $height)
if ($width_orig < $width) {
$width = $width_orig;
$height = $height_orig;
// otherwise, if the image is taller than it is wide and wider than our max
} else if ($width && ($width_orig < $height_orig)) {
// we set the width equal to the max height divided by the original height,
// times the original width - it's a fancy way of getting a proportional dimension
$width = ($height / $height_orig) * $width_orig;
} else {
// otherwise, our height is the one that's larger, so we apply the same math
// as above to the height instead of the width
$height = ($width / $width_orig) * $height_orig;
}
// think of the above two as landscape versus portrait
// the width is figured for landscape images
// the height is figured for portrait images

// Here, we are creating the image - if it's an unsupported type, we output
// a blank image with a message
// Create new image
$image_p = imagecreatetruecolor($width, $height);

// Determine the mimetype and use appropriate function
switch($mimetype) {
case '1' :
if (imagetypes() & IMG_GIF) {
$image = imagecreatefromgif($filename);
} else {
$image = imagecreate(200, 30);
// white background and blue text
$bg = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 0, 0, 180);
// write the string at the top left
imagestring($image, 5, 0, 0, "Filetype not supported!", $textcolor);
}
break;
case '2' :
if (imagetypes() & IMG_JPG) {
$image = imagecreatefromjpeg($filename);
} else {
$image = imagecreate(200, 30);
// white background and blue text
$bg = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 0, 0, 180);
// write the string at the top left
imagestring($image, 5, 0, 0, "Filetype not supported!", $textcolor);
}
break;
case '3' :
if (imagetypes() & IMG_PNG) {
$image = imagecreatefrompng($filename);
} else {
$image = imagecreate(200, 30);
// white background and blue text
$bg = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 0, 0, 180);
// write the string at the top left
imagestring($image, 5, 0, 0, "Filetype not supported!", $textcolor);
}
break;
default :
break;
}
// Resample
// this function copies our original image into a new image using the width
// and height we figured earlier
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>
[/syntax:5a1f6f0dfc]

Corey Bryant
December 23rd, 2004, 21:35
Thanks! He actually also just came back to me - both of y'all are fast ;) - and said that he was working on making the resizing proportional. he just did not think I was in a hurry. He is going to look at this code though to see what ideas might be taken.

For the most part - I was just posting as how much communication can be shut off between all the people. I was just surprised that this customer even knew how to crop or something.

sonicgroup
December 23rd, 2004, 21:38
Ah, then the important lesson is learned - Do not trust that your customers know what you want from them.

Rather than specifying the size of the images, in the future, you should probably specify uncropped images. ;)

Corey Bryant
December 23rd, 2004, 21:44
LOL - and the biggest problem - they are a big manufacturer of women's shoes. Of course - something that I know nothing about - barely enough men's shoes.

And they want their site to "look" like one of their distributors, so in the middle of that - I am fighting that battle also.