chenloong Posted June 12, 2007 Share Posted June 12, 2007 Hi to everyone, currently i working with a simple operation, where i need to extract a portion of images (200*200, starting at coordinate-x 100, and coordinate-y 100) and save it. Below is my code. But it not works, can anyone help on this? list($r_width, $r_height) = getimagesize("abc.jpg"); $target_path = "result.jpg"; $background = imagecreatetruecolor(200, 200); $image = imagecreatefromjpeg("abc.jpg"); imagecopyresampled($background, $image, 0, 0, 100, 100, 200, 200, $r_width, $r_height); imagejpeg($background, $target_path, 100); regards, chenloong Quote Link to comment https://forums.phpfreaks.com/topic/55246-how-to-extract-a-portion-of-an-image/ Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 What do you mean by "save it"? What do you want to do with it? Quote Link to comment https://forums.phpfreaks.com/topic/55246-how-to-extract-a-portion-of-an-image/#findComment-273070 Share on other sites More sharing options...
chenloong Posted June 12, 2007 Author Share Posted June 12, 2007 "save it" carry not much meaning,,,, it represent this code : imagejpeg($background, $target_path, 100); the main thing is i don't know how to extract the portion of image by given coordinate-x and y (starting point of the portion), width and height of the portion needed... regards, chenloong Quote Link to comment https://forums.phpfreaks.com/topic/55246-how-to-extract-a-portion-of-an-image/#findComment-273074 Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 Try replacting the first line with this: $size=getimagesize("abc.jpg"); Then change this line: imagecopyresampled($background, $image, 0, 0, 100, 100, 200, 200, $size[0], $size[1]); I've not played with imagecopyresampled so it's just a guess. Quote Link to comment https://forums.phpfreaks.com/topic/55246-how-to-extract-a-portion-of-an-image/#findComment-273082 Share on other sites More sharing options...
chenloong Posted June 12, 2007 Author Share Posted June 12, 2007 every parameter given there work fine, i has tested it.... the code is just a reference only.... it is not actually show the right way to do it.... let us forget about the code i wrote,,,, my requirement very simple. by given start coordinate (x,y), height, width of the portion, just extract the image from a source. I has attached an image, it shall make you all understand more. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/55246-how-to-extract-a-portion-of-an-image/#findComment-273084 Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 You can also try this code: <?php header("Content-type: image/jpeg"); $size = getimagesize("abc.jpg"); $target_path = "result.jpg"; if ($background = imagecreatetruecolor(200, 200)) { if ($image = imagecreatefromjpeg("abc.jpg")) { if (imagecopyresampled($background, $image, 0, 0, 100, 100, 200, 200, $size[0], $size[1])) { imagejpeg($background, $target_path, 100); } else {echo 'Fail: imagecopyresampled';} } else {echo 'Fail: imagecreatefromjpeg';} } else {echo 'Fail: imagecreatetruecolor';} ?> If you save that out and upload it, try entering the link to it directly into the browser. If it makes an image you'll get the image. If it fails you should be told where the script failed. Quote Link to comment https://forums.phpfreaks.com/topic/55246-how-to-extract-a-portion-of-an-image/#findComment-273086 Share on other sites More sharing options...
chenloong Posted June 13, 2007 Author Share Posted June 13, 2007 hi Yesideez, i has tried what you recommended. But as previous, it do generate a image, but it is not the result as i want. It produce an image with the whole resource pict and resize it to 200*200 only...... not only a portion of the resource image.... Quote Link to comment https://forums.phpfreaks.com/topic/55246-how-to-extract-a-portion-of-an-image/#findComment-273613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.