Jump to content

[SOLVED] copy part of an image


php_joe

Recommended Posts

Hi,

 

I have an image on my site that I want to copy a chunk out of and save. Preferably I'd like a function that will just copy and save a section starting at an x/y location and ending at an x/y location in the image.

 

I have read the page on php.net for imagecopy() and I just can't seem to get it to work (I always have trouble with the image codes).

 

I keep getting this:

Warning: imagecopy(): supplied argument is not a valid Image resource

 

this is what I have so far:

<?
$img = "./arkijah.jpg";
list($w, $h, $type, $attr) = getimagesize($img);

$temp = imagecreatetruecolor($w, $h);
$white = imagecolorallocate( $temp, 255, 255, 255 );

$crop_top = '10';
$crop_left = '10';

imagecopy($temp, $img, 0, 0, $crop_top, $crop_left, $w, $h);
        
imagefill( $temp, 0, 0, $white );
?>

 

The only think I can think of is that $img is not a "truecolor" image, whatever that is. However, when I try to make it truecolor, I can't seem to get the height & width any more.

 

Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/68242-solved-copy-part-of-an-image/
Share on other sites

Ok, I got it working.

 

Thanks a lot!

 

<?
$filename= "./image.jpg";
list($w, $h, $type, $attr) = getimagesize($filename);
$src_im = imagecreatefromjpeg($filename);

$src_x = '0';   // begin x
$src_y = '0';   // begin y
$src_w = '100'; // width
$src_h = '100'; // height
$dst_x = '0';   // destination x
$dst_y = '0';   // destination y

$dst_im = imagecreatetruecolor($src_w, $src_h);
$white = imagecolorallocate($dst_im, 255, 255, 255);
imagefill($dst_im, 0, 0, $white);

imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);

header("Content-type: image/png");
imagepng($dst_im);
imagedestroy($dst_im);
?>

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.