imagecopyresampled not working not sure why - php

So I'm attempting to use imagecopyresampled to crop out a section of a photo so that I don't have to worry about my users uploading photos larger than intended to my website. Unfortunately I have yet to figure out why imagecopyresampled is basically behaving as though I simply resized the image using CSS. From my understanding it should only copy a section of the image at 0,0 based on the coordinates I've provided to a 325X300 px jpg.
!:example
The top image is the one I'm using imagecopyresampled to generate. My code is as follows. Just trying to understand what I'm doing wrong here because apparently my copy of GD doesn't have imagecrop otherwise I'd probably be using that.
<html>
<style>
.sample{
width: 325;
height: 300;
}
</style>
<body>
<?php
$image = imagecreatefromjpeg('Image6.jpg');
$filename = 'Thumbnail_Image6.jpeg';
$width = 325;
$height = 300;
$oldwidth = imagesx($image);
$oldheight = imagesy($image);
if( $oldwidth > 325 || $oldheight > 300){
$thumb = ImageCreateTrueColor( 325, 300);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, 325, 300, $oldwidth, $oldheight);
imagejpeg($thumb, $filename, 100);
echo "<img src='".$filename."'><br>";
echo "<img class='sample' src='Image6.jpg'><br>";
}
?>
</body>
</html>

if you're going to crop the image, you don't need to use full image size.
imagecopyresampled($thumb, $image, 0, 0, 0, 0, 325, 300, 325, 300);

Mainly because your source size is the full size of the source image, so it's resizing it instead of taking a chunk out. Try this to see what I mean:
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $width, $height);
You can offset the chunk by changing the source x and y values. Example:
imagecopyresampled($thumb, $image, 0, 0, 50, 50, $width, $height, $width, $height);

Related

PHP increase image resolution from url and then crop

I have images with resolution of 720x1280 and I would need to store them on server with resolution 800x1500.
Since 720x1280 increased to height 1500 gives resolution of 844x1500 I would also need to crop image, remove 22 pixels from the left and right side.
For now I have this:
$img_url = file_get_contents($url);
$img = imagecreatefromstring($img_url);
$width = imagesx($img);
$height = imagesy($img);
$new_width = '800';
$new_height = '1500';
$thumb = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($thumb, $name, 100);
imagedestroy($thumb);
imagedestroy($img);
But the problem is that image is not cropped, 22 pixels from the left and right side are not removed.
Is there a way to do this, to first increase image resolution from url and then crop?
Googling php image crop reveals the secret:
$rect = [22, 0, 800, 1500]
$thumb = imagecrop($thumb, $rect)
In this line:
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
you just scale your original image to new dimensions.
As the documentation says:
If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed.
You need to set:
$shiftX = 22*(1280/1500); // consider passing variables rather than constant values
$scaledWidth = 720-$shiftX*2;
imagecopyresampled($thumb, $img, 0, 0, $shiftX, 0, $new_width, $new_height, $scaledWidth, $height);
which cuts out the required (proportional) rectangle of the source image to paste it in you destination picture.

Place an image to the center of another image using PHP GD

I need to place an image to the center of another image(both horizontally and vertically) with dimension 700*350. I'm trying with the following code. But I'm getting image as stretched.
#header("Content-Type: image/png");
$imageURL = "flower.jpg";
// create a transparent background image for placing the $imageURL image
$imageResource = imagecreatetruecolor(700, 350);
imagesavealpha($imageResource, true);
$transparentColor = imagecolorallocatealpha($imageResource, 0, 0, 0, 127);
imagefill($imageResource, 0, 0, $transparentColor);
$backgroundImage = imagecreatefromjpeg($imageURL);
list($width, $height) = getimagesize($imageURL);
imagecopyresampled($imageResource, $backgroundImage, 350, 175, 0, 0, 700, 350, $width, $height);
imagepng($imageResource, "newimage.jpg");
This is not centering the image and also the file flower.jpg is getting deleted when I run this code. What I'm doing wrong in this?
Can anyone please help me to fix this? Thanks in advance.
So you need something like this?
#header("Content-Type: image/png");
$imageURL = "flower.jpg";
// create a transparent background image for placing the $imageURL image
$imageResource = imagecreatetruecolor(700, 350);
imagesavealpha($imageResource, true);
$transparentColor = imagecolorallocatealpha($imageResource, 0, 0, 0, 127);
imagefill($imageResource, 0, 0, $transparentColor);
$backgroundImage = imagecreatefromjpeg($imageURL);
list($width, $height) = getimagesize($imageURL);
imagecopyresampled($imageResource, $backgroundImage, 175, 85, 0, 0, 350, 175, $width, $height);
imagepng($imageResource, "newimage.jpg");
imagedestroy($imageResource);
imagedestroy($backgroundImage);
You had specified the center of the destination image as the destination coordinates and the whole destination image size instead of needed dimentions of the center rectangle into which the source image would be resized.
Also you didn't do imagedestroy, which you totally should.

Resize image - Keep proportion - Add white background

I want to resize my images to a square. Say I want a squared image of 500x500 and I have an image of 300x600
I want to resize that image down to 200x500 and then add a white background to it to make it 500x500
I got something working good by doing this:
$TargetImage = imagecreatetruecolor(300, 600);
imagecopyresampled(
$TargetImage, $SourceImage,
0, 0,
0, 0,
300, 600,
500, 500
);
$final = imagecreatetruecolor(500, 500);
$bg_color = imagecolorallocate ($final, 255, 255, 255)
imagefill($final, 0, 0, $bg_color);
imagecopyresampled(
$final, $TargetImage,
0, 0,
($x_mid - (500/ 2)), ($y_mid - (500/ 2)),
500, 500,
500, 500
);
It's doing almost EVERYTHING right. The picture is centered and everything. Except the background is black and not white:/
Anyone know what I'm doing wrong?
I think this is what you want:
<?php
$square=500;
// Load up the original image
$src = imagecreatefrompng('original.png');
$w = imagesx($src); // image width
$h = imagesy($src); // image height
printf("Orig: %dx%d\n",$w,$h);
// Create output canvas and fill with white
$final = imagecreatetruecolor($square,$square);
$bg_color = imagecolorallocate ($final, 255, 255, 255);
imagefill($final, 0, 0, $bg_color);
// Check if portrait or landscape
if($h>=$w){
// Portrait, i.e. tall image
$newh=$square;
$neww=intval($square*$w/$h);
printf("New: %dx%d\n",$neww,$newh);
// Resize and composite original image onto output canvas
imagecopyresampled(
$final, $src,
intval(($square-$neww)/2),0,
0,0,
$neww, $newh,
$w, $h);
} else {
// Landscape, i.e. wide image
$neww=$square;
$newh=intval($square*$h/$w);
printf("New: %dx%d\n",$neww,$newh);
imagecopyresampled(
$final, $src,
0,intval(($square-$newh)/2),
0,0,
$neww, $newh,
$w, $h);
}
// Write result
imagepng($final,"result.png");
?>
Note also, that if you want to scale down 300x600 to fit in 500x500 whilst maintaining aspect ratio, you will get 250x500 not 200x500.

Want to use imagecopy or imagecopyresampled with 50% opacity

I am using PHP GD imagecopy or imagecopyresampled to merge a PNG image with transparency onto another PNG image (basically creating t-shirt mockups with design).
When I use imagecopymerge() it doesn't respect the transparency. but opacity setting option works. But when I use imagecopy() or imagecopyresampled() then transparency works but opacity setting is not available.
So how to merge images which respects transparency and provides 50% opacity as well?
My code is:
$img1 = imagecreatefrompng('m1.png');
$img2 = imagecreatefrompng('m2.png');
imagealphablending( $img2, false );
imagesavealpha( $img2, true );
$x1 = imagesx($img1);
$y1 = imagesy($img1);
$x2 = imagesx($img2);
$y2 = imagesy($img2);
//imagecopyresampled($img1, $img2, 205, 170, 0, 0, $x2-40, $y2-40, $x2, $y2);
imagecopy($img1, $img2, 205, 170, 0, 0, $x2-40, $y2-40);
header('Content-Type: image/png');
imagepng($img1);
Please help.
You can have a look at this link:
https://bugs.php.net/bug.php?id=23815
imagecopymerge doesn't support image alpha. Becuase of that, they have requested the creation of a new function imagecopymergealpha to do this kind of job.
https://github.com/php/php-src/pull/211
imagecopymerge($img1, $img2, 0, 0, 0, 0, $x1, $y1, 50);
header('Content-Type: image/png');
imagepng($img1);
Please check using this sample code
$imageName = 'path/to/your/image/file'
$im_src = create_image_from_type($imageName);
$size = getimagesize($imageName);
$im_dst = create_image_from_type($imageName);
$white = imagecolorallocate($im_dst, 255, 255, 255);
imagecolortransparent($im_dst, $white);
imagefilledrectangle($im_dst, 0, 0, $size[0], $size[1], $white);
$opacityVal = 50;// put the opacity value here
imagecopymerge($im_dst, $im_src, 0, 0, 0, 0,$size[0], $size[1], $opacityVal);
save_image($im_dst, $imageName, 100);

PHP - cropping image with imagecopyresampled()?

I'd like to crop an image using imagecreatetruecolor and it always crops it leaving black spaces, or the zoom is too big. I want the image to be exactly 191px wide and 90px high, so I also need to resize the image, as well as crop, because the ratio has to be kept. Well, there are some samples of the project:
The resize script (simplified) goes like this:
$src_img=imagecreatefromjpeg($photoTemp);
list($width,$height)=getimagesize($photoTemp);
$dst_img=imagecreatetruecolor(191, 90);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height);
The $newImage['crop'] array includes:
['x'] => $_POST['inp-x']
['y'] => $_POST['inp-x']
['width'] => $_POST['inp-width']
['height'] => $_POST['inp-height']
But what I get is:
Anyone sees, what I'm doing wrong?
Thanks, Mike.
you can do it too, I myself did it, and it works
(x1,y1)=> where crop starts
(x2,y2)=> where crop ends
$filename = $_GET['imageurl'];
$percent = 0.5;
list($width, $height) = getimagesize($filename);
$new_width = $_GET['x2'] - $_GET['x1'];
$new_height = $_GET['y2'] - $_GET['y1'];
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0 , $_GET['x1'] , $_GET['y1'] , $new_width, $new_height, $new_width, $new_height);
// Outputs the image
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
Try
<?php
$dst_img = imagecreatetruecolor($newImage['crop']['width'], $newImage['crop']['height']);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], 0, 0, $width, $height);
Ok, I found the problem myself, the code should be like this:
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']);
There's also the imagecrop function, which allows you to pass in an array with x, y, width, and height values.

Categories