Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/55246-how-to-extract-a-portion-of-an-image/
Share on other sites

"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

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.

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.Sunset.jpg

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.

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....

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.