Jump to content

Merging image crop with image resize


TeddyKiller

Recommended Posts

I have two scripts, one which is image crop (always square) and one which is image resize, (always square)

I'm looking to merge them together. I haven't tested the crop yet. So any corrections feel free to make.

 

The image is grabbed by the URL of the image SRC.

What happens is, it gets the file, passes it through the crop and crop it, and the outcome image goes through the resize.

 

Cropping Script

<?php
$pic = $_GET['file'];
list($current_width, $current_height) = getimagesize($pic);

if($current_width > $current_height){
$crop_width = $current_height;
$crop_height = $current_height;
}
elseif($current_height > $current_width){
$crop_width = $current_width;
$crop_height = $current_width;
)

$sum1 = $current_width - $crop_width;
$left = $sum1 / 2;

$sum2 = $current_height - $height;
$top = $sum2 / 2;

$current_image = imagecreatefromjpeg($pic);
$canvas = imagecreatetruecolor($crop_width, $crop_height);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);

header('Content-type: image/jpeg');
imagejpeg($dst,NULL,100);
imagedestroy($current_image);
imagedestroy($canvas);
?> 

 

Resize script

<?php 
$pic = $_GET['file'];

if (!isset($max_width))
    $max_width = 90;
if (!isset($max_height))
    $max_height = 90;
  	
list($width, $height) = getimagesize($pic);

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if (($width <= $max_width) && ($height <= $max_height)){
$tn_width = $width;
$tn_height = $height;
} 
elseif (($x_ratio * $height) < $max_height){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}

$src = imagecreatefromjpeg($pic);
$dst = imagecreatetruecolor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width,$height);

header('Content-type: image/jpeg');
imagejpeg($dst,NULL,100);
imagedestroy($src);
imagedestroy($dst);
?>

 

Hope you can help, shouldn't be a huge change. It is important that the cropping becomes before the resize though!

Link to comment
https://forums.phpfreaks.com/topic/194969-merging-image-crop-with-image-resize/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.