tags:

views:

301

answers:

1

hi, here is the website im talking about http://makeupbyarpi.com/portfolio.php

you'll notice some of the images are smushed width-wise.

the code i used is this:

$width="500";
$height="636";


 $img_src = $_FILES['galleryimg']['tmp_name'];
 $thumb = "../gallery/".rand(0,100000).".jpg";


 //Create image stream 
 $image = imagecreatefromjpeg($img_src);

 //Gather and store the width and height
 list($image_width, $image_height) = getimagesize($img_src);


 //Resample/resize the image 
 $tmp_img = imagecreatetruecolor($width, $height);
 imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);

 //Attempt to save the new thumbnail 
 if(is_writeable(dirname($thumb))){
  imagejpeg($tmp_img, $thumb, 100);

 }

 //Free memory 
 imagedestroy($tmp_img);
 imagedestroy($image);

the images that get uploaded are huge sometimes 3000px by 2000px and i have php crop it down to 500 x 536 and some landscape based images get smushed. is there a formula i can use to crop it carefully so that the image comes out good?

thanks

A: 

You could resize and add a letterbox if required. You simply need to resize the width and then calculate the new height (assuming width to height ratio is same as original) then if the height is not equal to the preferred height you need to draw a black rectangle (cover background) and then centre the image.

You could also do a pillarbox, but then you do the exact same as above except that width becomes height and height becomes width.

Edit: Actually, you resize the one that is the biggest, if width is bigger, you resize that and if height is bigger then you resize that. And depending on which one you resize, your script should either letterbox or pillarbox.

EDIT 2:

<?php
    // Define image to resize 
    $img_src = $_FILES['galleryimg']['tmp_name'];
    $thumb = "../gallery/" . rand(0,100000) . ".jpg";

    // Define resize width and height
    $width = 500;
    $height = 636;

    // Open image
    $img = imagecreatefromjpeg($img_src);

    // Store image width and height
    list($img_width, $img_height) = getimagesize($img_src);

    // Create the new image
    $new_img = imagecreatetruecolor($width, $height);

    // Calculate stuff and resize image accordingly
    if (($width/$img_width) < ($height/$img_height)) {
     $new_width = $width;
     $new_height = ($width/$img_width) * $img_height;
     $new_x = 0;
     $new_y = ($height - $new_height) / 2;
    } else {
     $new_width = ($height/$img_height) * $img_width;
     $new_height = $height;
     $new_x = ($width - $new_width) / 2;
     $new_y = 0;
    }

    imagecopyresampled($new_img, $img, $new_x, $new_y, 0, 0, $new_width, $new_height, $img_width, $img_height);

    // Save thumbnail
    if (is_writeable(dirname($thumb))) {
     imagejpeg($new_img, $thumb, 100);
    }

    // Free up resources
    imagedestroy($new_img);
    imagedestroy($img);
?>

Sorry it took a while, I ran across a small bug in the calculation part which I was unable to fix for like 10 minutes =/ This should work.

Sbm007
so if the width is bigger than the height in this case i set the width to 500 and leave the height?
iaddesign
Give me 5 minutes I'll write up an example.
Sbm007
wow thats pretty good. thank you so much! you dont have to appologise for it taking long u did me a big favor. i wish i can up your points :)
iaddesign
Then upvote his answer:)
Quamis