Jump to content

PHP and GD problems


silasl

Recommended Posts

Hi all, I'm having a probblem with a GD resize / crop script and i'm wondering if anyone can help me.

The code is supposed to take an uploaded image, resize it to the correct width, retaining its aspect ration, and then crop it. Currently it crops it fine and it's doing some resizing but with it's ignoring the dimensions i'm asking for and just shrinking it a little bit, it's also distorting the image...

 

Can anyone see what's wrong?

 

$rWidth = 532;
$rHeight = 100;
$name = ( $_FILES['userfile']['name'] );
if ($name){
	$target_path = "../../images/page_images/";
	$target_path = $target_path . basename( $_FILES['userfile']['name']);
	move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path);
	$dimensions = getimagesize($target_path); 
	if ($dimensions[0] != $rWidth && $dimensions[1] != $rHeight){
		$canvas = imagecreatetruecolor($rWidth,$rHeight);
		$piece = imagecreatefromjpeg($target_path);
		$width = $dimensions[0]; 
		$height = $dimensions[1]; 

		$newWidth = $rWidth;  
		$newHeight = round($rWidth/($width/$height));

		imagecopyresized($canvas, $piece, 0, 0, 0, 0, $rWidth, $rHeight, $newWidth, $newHeight);
		imagejpeg($canvas,$target_path,90);
	}
}

 

Thanks in advance for your help.

 

Silas

 

Link to comment
Share on other sites

GD is great at distorting images have you thought of using ImageMagick?

Really! GD works fine for me.

 

Try the following code:

<?php

if(isset($_POST['Submit']))
{
    // define the max width and height of uploaded images
    $max_width  = 532;
    $max_height = 100;

    // define folder inwhich the images get uploaded to
    $upload_dir = '../../images/page_images/';

    // get uploaded image data from form
    $file_name = $_FILES['image']['name'];
    $file_tmp  = $_FILES['image']['tmp_name'];
    $file_size = $_FILES['image']['size'];
    $file_type = $_FILES['image']['type'];

    // get info about uploaded image
    list($width, $height, $type, $attr) = getimagesize($file_tmp);

    // set the full upload path for where to upload the image to.
    $img_path = $upload_dir . $file_name;

    // check to see if the uploaded image is bigger tha max width an height
    // if it is we'll crop the image
    if($width > $max_width && $height > $max_height)
    {
        // image is too big so we'll crop it

        // get the aspect ratio of the image
        // we do this so we can keep the aspect ratio when the image gets cropped.
        $aspect_ratio = $height / $width;
        $new_width    = $max_height / $aspect_ratio;

        // create a blank canvas for the size of the image
        // this is what the image will get cropped to
        $img_rs = imagecreatetruecolor($new_width, $max_height);

        // setup a image resource to the uploaded file
        $img = imageCreateFromJPEG($file_tmp);

        // crop the image
        imagecopyresampled($img_rs, $img, 0, 0, 0, 0, $new_width, $max_height, ImageSX($img), ImageSY($img));

        // create the image
        ImageJPEG($img_rs, $img_path);

        // destroy the temporary image
        imagedestroy($img_rs);
    }
    else
    {
        // uploaded image is under the max width and hieght
        // we'll just upload it straight away
        move_uploaded_file($file_tmp, $img_path);
    }

    // display the image
    echo "<h1>Upload Image:</h1>\n\n<img src=\"$img_path\">";
}
else
{
    echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '" enctype="multipart/form-data">
    <input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}

?>

Link to comment
Share on other sites

Thanks for your reply, that script seems to work, as in it's resizing the image without distorting, but its shrinking it down to the height of 100 and resizing the width accordingly so it's ignoring the 532 width, I need it to retain the width and then crop whatever's left to 100 height. So i'm left with a thumbnail that's 532 x 100. Did I explain that OK...?

 

Cheers,

 

Silas

 

Link to comment
Share on other sites

The following code will crop it width ways, so any image that is greater than 532 pixels wide will get to resized to 532px wide by what ever the height based on the aspect ratio. It wont resize it to 532 x 100

<?php

if(isset($_POST['Submit']))
{
    // define the max width and height of uploaded images
    $max_width  = 532;
    $max_height = 100;

    // define folder inwhich the images get uploaded to
    $upload_dir = './pics/';

    // get uploaded image data from form
    $file_name = $_FILES['image']['name'];
    $file_tmp  = $_FILES['image']['tmp_name'];
    $file_size = $_FILES['image']['size'];
    $file_type = $_FILES['image']['type'];

    // get info about uploaded image
    list($width, $height, $type, $attr) = getimagesize($file_tmp);

    // set the full upload path for where to upload the image to.
    $img_path = $upload_dir . $file_name;

    // check to see if the uploaded image is bigger tha max width an height
    // if it is we'll crop the image
    //if($width > $max_width && $height > $max_height)
    if($width > $max_width)
    {
        // image is too big so we'll crop it

        // get the aspect ratio of the image
        // we do this so we can keep the aspect ratio when the image gets cropped.
        $aspect_ratio = $width / $height;
        $new_height   = $max_width / $aspect_ratio;

        // create a blank canvas for the size of the image
        // this is what the image will get cropped to
        $img_rs = imagecreatetruecolor($max_width, $new_height);

        // setup a image resource to the uploaded file
        $img = imageCreateFromJPEG($file_tmp);

        // crop the image
        imagecopyresampled($img_rs, $img, 0, 0, 0, 0, $max_width, $new_height, ImageSX($img), ImageSY($img));

        // create the image
        ImageJPEG($img_rs, $img_path);

        // destroy the temporary image
        imagedestroy($img_rs);
    }
    else
    {
        // uploaded image is under the max width and hieght
        // we'll just upload it straight away
        move_uploaded_file($file_tmp, $img_path);
    }

    // display the image
    echo "<h1>Upload Image:</h1>\n\n<img src=\"$img_path\">";
}
else
{
    echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '" enctype="multipart/form-data">
    <input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.