Jump to content

Recommended Posts

Hi, I'm trying to resize and then crop an image. I found the following code for resizing an image.

 

<?php
// The file
$filename = $_GET['src'];

// Set a maximum height and width
$width = 256;
$height = 1000;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

imagejpeg($image_p, null, 100);

?>

 

I thought that I could put the result into a variable like

 

$resized = imagejpeg($image_p, null, 100);

 

to then use with this code for cropping the image. Replacing the $_GET with the $resized variable

 

<?php
$x=0;
$y=0;
$filename=$_GET['src'];
header('Content-type:image/jpg');
header('Content-Disposition: attachment;filename='.$src);
list($width, $height, $type, $attr) = getimagesize($filename);
$w= $width ;
$h= 86;
$image=imagecreatefromjpeg($filename);
$crop=imagecreatetruecolor($w,$h);
imagecopy($crop,$image,0,0,$x,$y,$w,$h);
imagejpeg($crop);
?>

 

It seems though that I am wrong.  :shrug: Could someone help me with this? Thanks!  :)

Link to comment
https://forums.phpfreaks.com/topic/183061-resizing-and-then-cropping-an-image/
Share on other sites

As your not writing the image back to a file the second code won't work, try this instead

 

replace

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, null, 100);

?>

<?php
$x=0;
$y=0;
$height= 86;
imagecopyresampled($image_p, $image, 0, 0, $x, $y, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p);
?>

Thanks MadTechie! I took what you said and fixed it a little to get the result I wanted. For anyone else interested this is the code.

 

<?php
// The file
$filename = $_GET['src'];

// Set a maximum height and width
$width = 256;
$height = 1000;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

$height = 83;

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);

$x=0;
$y=0;
$width=256;
$height_orig = $height * $ratio_orig;
imagecopyresampled($image_p, $image, 0, 0, $x, $y, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p);
?>

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.