I'm trying to crop of the bottom part of an image, which i get from a remote site. Got it also working with the following code:
$u = $xmlString->xpath('//*[contains(@u, "/fds/")]');
foreach($u as $result) {
$itemLinks = 'http://exampleurl/'.$result['u'].'.png';
$in_filename = $itemLinks;
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 264;
$new_width = $width;
$image = imagecreatefrompng($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$transparentindex = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefill($new_image, 0, 0, $transparentindex);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header("Content-Type: image/png");
imagepng($new_image);
}
The only problem with this code is the following:
I'm getting the Image Path from a remote XML file, which i filtered with xpath. So all my finished Image url's are stored in an array. But my code is just generating 1 image which contains the perfect size which i need.
It happens because its just generating 1 img in the end. Maybe also happens because it just returns 1 image with the name img.
Question: Does anyone have a idea why it wouldnt return all images?
For example:
- Array contains 15 image links.
- Im running my foreach loop through the array.
- Foreach loop returns only 1 image.