Jump to content

How would I combine these two image scripts


TeddyKiller

Recommended Posts

I have two scripts. They both work on there own. One is cropping, one is resizing.

(An image should always be cropped before resizing)

As the work, I basically would need to set a variable at the end of script one, to pass it through to script two. That's all. It's a simple thing, however.. I can't do it.

 

The image URL is gotten from the URL, the image has to run through both scripts and come out at the end as it normally would with one script.

 

Cropping

<?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($canvas,NULL,100);
imagedestroy($current_image);
imagedestroy($canvas);
?> 

 

Resizing

<?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);
?>

 

Can you help?

Thanks

Link to comment
Share on other sites

Not exactly sure what you want cause you dont save just display but try this:

 

at the start of #1 set a $timestamp

then when finished save the output to $timestamp.jpg

 

in #2 change the $pic to $pic = $timestamp.jpg

remove the header from #2

 

in #1 include #2 at the end

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Teamatonic, that can't be done.

The images are random. So you'll end up with a folder of a billion images.

 

I came up with this, although this doesn't output the image.

http://horble.com/test/test.php

Any help? Code below.

 

<?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');
$cropimage = imagejpeg($canvas,NULL,100);
imagedestroy($current_image);
imagedestroy($canvas);

$pic = $cropimage;
   
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);
?>

Link to comment
Share on other sites

I hate say it but you could bang your head against that for a year and it would not work that way. You have to drop the first image creation and the first thumb getsize as its not really a resource. Then use the crop_values instead of the width/height. I whacked out what you didnt need and changed it it use the crop values. It does however change the crop position so you will have to adjust that to suit where you want it.

 

<?php
$pic = './test.jpg';

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;

if (!isset($max_width))
    $max_width = 90;
if (!isset($max_height))
    $max_height = 90;


$x_ratio = $max_width / $crop_width;
$y_ratio = $max_height / $crop_height;

if (($crop_width <= $max_width) && ($crop_height <= $max_height)){
   $tn_width = $crop_width;
   $tn_height = $crop_height;
}
elseif (($x_ratio * $crop_height) < $max_height){
   $tn_height = ceil($x_ratio * $crop_height);
   $tn_width = $max_width;
} else {
   $tn_width = ceil($y_ratio * $crop_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, $crop_width,$crop_height);

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

 

 

HTH

Teamatomic

Link to comment
Share on other sites

The crop script centers the crop, and I've had a look.. and some of it I don't understand.

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

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

This is suppose to center the crop. $left and $top variables aren't being used. So where would they go.

Can you center it for me?

Also, possible comment the lines so I can understand them :)

 

thanks

Link to comment
Share on other sites

You only need one coords to anchor the crop area casue we square the crop so we only anchor it to the longest axis.

 

I just forgot to cut out that unused stuff.

 

<?php
$pic = './test.jpg';

list($width, $height) = getimagesize($pic);

if($width > $height) // so we can square up the crop area
{
$long_s = $width;
$short_s = $height;
}
else
{
$long_s = $height;
$short_s = $width;
}

if (!isset($max_width))
    $crop_width = 90;
if (!isset($max_height))
    $crop_height = 90;

$dim_1=($long_s - $short_s)/2; //top left cords in original to anchor the crop area


$src  = imagecreatefromjpeg($pic);
$dst = imagecreatetruecolor($crop_width, $crop_height);
imagecopyresampled($dst, $src, 0,0,0,$dim_1, $crop_width, $crop_height,$short_s, $short_s );

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

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Yup. I didnt allow for a already equal axis. Change the width/height if to this

 

if($width > $height) // so we can square up the crop area
{
$long_s = $width;
$short_s = $height;
}
elseif($width < $height)
{
$long_s = $height;
$short_s = $width;
}
else
{
$long_s = $height;
$short_s = $width;
}

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Doesn't work. Would it be possible to add in a..

if($width > $height || $width < $height){

Do stuff here

}

 

There for, if the width and height is already square and equal, it would still go through resize?

 

EDIT:

That made stuff worse without having the variables in. Even with your method, for some reason that image just wont load. (The other images the one iwth a "No Photo" there perectly square anyway so..

So i'm imagining it's the graphics of the image?

Link to comment
Share on other sites

Doesn't work. Would it be possible to add in a..

if($width > $height || $width < $height){

Do stuff here

}

 

There for, if the width and height is already square and equal, it would still go through resize?

 

EDIT:

That made stuff worse without having the variables in. Even with your method, for some reason that image just wont load. (The other images the one iwth a "No Photo" there perectly square anyway so..

So i'm imagining it's the graphics of the image?

 

what the hell...

 

Your checking if width is bigger than height and if height is bigger than width...

 

You're gonna get a false for that.

Link to comment
Share on other sites

@Ruzzas

Wrong. It would always be true unless the axis are equal.

 

@TeddyKiller

What the little block of code does it to determine of the graphic is(think printing) page, landscape, or square. That way there is a starting point to lay in the crop so it is centered. With a square image it does not matter which side is declared. Which that block does just fine.

 

I have tested it with equal axis images and it works as intended. If there is a problem it must lie within the image itself. Perhaps the image is a gif type, the script does  not allow for non jpg images to be used.

To do so would be easy, just use if's where there are jpg specific functions to select the proper type function and work from the original image extension. The final process could then save it to whatever type you want.

 

 

HTH

Teamatomic

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.