php_joe Posted September 6, 2007 Share Posted September 6, 2007 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 More sharing options...
Barand Posted September 6, 2007 Share Posted September 6, 2007 $img needs to be a gd image $img = imagecreatefromjpeg("./arkijah.jpg"); Link to comment https://forums.phpfreaks.com/topic/68242-solved-copy-part-of-an-image/#findComment-343060 Share on other sites More sharing options...
php_joe Posted September 6, 2007 Author Share Posted September 6, 2007 I tried that. when I use the code that you posted I get: Warning: getimagesize(Resource id #4): failed to open stream: No such file or directory Link to comment https://forums.phpfreaks.com/topic/68242-solved-copy-part-of-an-image/#findComment-343077 Share on other sites More sharing options...
Barand Posted September 6, 2007 Share Posted September 6, 2007 Sorry! What I said was right, but getimagesize needs filename $filename= "./arkijah.jpg"; list($w, $h, $type, $attr) = getimagesize($filename); $img = imagecreatefromjpeg($filename); Link to comment https://forums.phpfreaks.com/topic/68242-solved-copy-part-of-an-image/#findComment-343081 Share on other sites More sharing options...
php_joe Posted September 8, 2007 Author Share Posted September 8, 2007 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); ?> Link to comment https://forums.phpfreaks.com/topic/68242-solved-copy-part-of-an-image/#findComment-344242 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.