[GD] problem with width and height

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mm88
Forum Newbie
Posts: 4
Joined: Tue Nov 17, 2009 10:43 am

[GD] problem with width and height

Post by mm88 »

Hi guys, this is my first post.

I'm using this script in a page called crop.php

Code: Select all

 
<? 
header ("Content-type: image/jpeg");
$file_name=$_GET['f'];
$crop_height=$_GET['h'];
$crop_width=$_GET['w'];
$file_type= explode('.', $file_name); 
$file_type = $file_type[count($file_type) -1];
$file_type=strtolower($file_type);
 
$original_image_size = getimagesize($file_name);
$original_width = $original_image_size[0];
$original_height = $original_image_size[1];
 
if($file_type=='jpg') 
{
$original_image_gd = imagecreatefromjpeg($file_name);
}
 
if($file_type=='gif') 
{ $original_image_gd = imagecreatefromgif($file_name);
} 
 
if($file_type=='png') 
{
$original_image_gd = imagecreatefrompng($file_name);
}
 
$cropped_image_gd = imagecreatetruecolor($crop_width, $crop_height);
$wm = $original_width /$crop_width;
$hm = $original_height /$crop_height;
$h_height = $crop_height/2;
$w_height = $crop_width/2;
 
if($original_width > $original_height ) 
{
$adjusted_width =$original_width / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
 
imagecopyresampled($cropped_image_gd ,$original_image_gd ,-$int_width,0,0,0, $adjusted_width, $crop_height, $original_width , $original_height );
} 
elseif(($original_width < $original_height ) || ($original_width == $original_height ))
{
$adjusted_height = $original_height / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
 
imagecopyresampled($cropped_image_gd , $original_image_gd ,0,-$int_height,0,0, $crop_width, $adjusted_height, $original_width , $original_height );
} 
else {
 
imagecopyresampled($cropped_image_gd , $original_image_gd ,0,0,0,0, $crop_width, $crop_height, $original_width , $original_height );
}
imagejpeg($cropped_image_gd); 
 
?>
 

I crop an image using this tag:

Code: Select all

 
<img src="crop.php?h=100&w=100&f=pics/image.jpg" />
 

this is the problem:

if my image is a 500 x 450 px and I crop it with height 500px and width 50px, the crop function works, but cropping it with height 50px and width 500px, the crop function doesn't work and I see something like that:

Image

The script resizes the whole image, adding a black background.

Why?

thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [GD] problem with width and height

Post by requinix »

That code's more along the lines of resizing.

Cropping is easy. There's practically no math involved: one call to imagecreatetruecolor and one call to imagecopy. That's it.
mm88
Forum Newbie
Posts: 4
Joined: Tue Nov 17, 2009 10:43 am

Re: [GD] problem with width and height

Post by mm88 »

tasairis wrote:That code's more along the lines of resizing.

Cropping is easy. There's practically no math involved: one call to imagecreatetruecolor and one call to imagecopy. That's it.

Ok, thanks for your reply, but why that script works only in a way ?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [GD] problem with width and height

Post by requinix »

mm88 wrote:but why that script works only in a way ?
What?

The code you have now is doing stuff like division and subtraction. There's no need for that. If all you're doing is cropping then
- Create a new image of the size you want
- Figure out where to crop the original image
- Do an imagecopy
mm88
Forum Newbie
Posts: 4
Joined: Tue Nov 17, 2009 10:43 am

Re: [GD] problem with width and height

Post by mm88 »

tasairis wrote:
mm88 wrote:but why that script works only in a way ?
What?

The code you have now is doing stuff like division and subtraction. There's no need for that. If all you're doing is cropping then
- Create a new image of the size you want
- Figure out where to crop the original image
- Do an imagecopy

Thanks for your reply, but I don't manage the GD functions well, so I've copied that script.

Could you paste here an example of your script?


many thx.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

You really should learn about it.

The worst thing a person new to PHP (or any language) to do is start messing with large pieces of code, copying and pasting all over the place. Read some tutorials, write some small code, and work your way up.

:)
mm88
Forum Newbie
Posts: 4
Joined: Tue Nov 17, 2009 10:43 am

Re:

Post by mm88 »

Jonah Bron wrote:You really should learn about it.
Thanks for your reply, but I haven't found a tutorial about the singular crop function.

A tutorial about all GD functions is completely useless to me.

tasairis speaks about only 3 functions to crop the image, so I've kindly asked him to paste here an example.

thanks.
Post Reply