PHP - cropping image with imagecopyresampled()? - php

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.

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.

php merge image with it own fliped and rotated view at different positions

i want to merge image with it own flipped image and rotated at angle say -60 degree.
Suppose i have image of 1000*1000 px then
A: original image resized to 500*500px and places in right bottom part of 1000*1000 square box
B:same image a flipped and rotated by -600 degree and place in top left corner of 1000*1000 square box
everything works fine but just issue like
1: some border for rotated images
2: background on rotate one is more darker make it look bid odd
test case :
sample image (thumbnail is resized, click to open original image) :
output image :
Below is my code
$image1=$image2=imagecreatefrompng('a.png');
//filter_opacity( $image1, 25 );
$w=imagesx($image1);
$h=imagesy($image1);
$finala = imagecreatetruecolor($w, $h);
$finalb = imagecreatetruecolor($w, $h);
$finalc = imagecreatetruecolor($w, $h);
$backgroundColora = imagecolorallocate($finala, 250,252,252); // gray
$backgroundColorb = imagecolorallocate($finalb, 250,250,250); // gray
$backgroundColorc = imagecolorallocate($finalc, 250,250,250); // gray
imagefill($finala, 0, 0, $backgroundColora);
imagefill($finalb, 0, 0, $backgroundColorb);
imagefill($finalc, 0, 0, $backgroundColorc);
$percent = 0.583;
$new_width = $w * $percent;
$new_height = $h * $percent;
$wshift = $w/8.5;
$hshift = $h/2.5;
imagecopy($finala, $image1, 0,0,0,0,$w,$h);
imagecopyresampled($finala, $image2,$wshift,$hshift,0,0, $new_width, $new_height, $w, $h);
imagecopy($finalc, $image1, 0,0,0,0,$w,$h);
imagecopyresampled($finalc, $image2,$wshift,$hshift,0,0, $new_width, $new_height, $w, $h);
imagecopyresampled($finalb, $finalc,$w*0.3,$h*0.3,0,0, $w*0.6, $h*0.6, $w, $h);
imageflip($finala, IMG_FLIP_HORIZONTAL );
$finala = imagerotate($finala, -60, imageColorAllocateAlpha($finala, 0, 0, 0, 127));
imagecopyresampled($finalb, $finala,-$w*0.1,-$h*0.1,0,$h*0.20, $new_width, $new_height, $w, $h);
header('Content-Type: image/jpeg');
imagejpeg($finalb);
imagedestroy($finala);
imagedestroy($image2);
imagedestroy($image1);
imagedestroy($final);
As you need to rotate and place same image at top and bottom, you no need to pass same image twice with $_GET single time is enough
// Source image
$image = imagecreatefrompng("test.png");
$w=imagesx($image);
$h=imagesy($image);
$finalImage = imagecreatetruecolor($w, $h);
// Bg add
$backgroundColor = imagecolorallocate($finalImage, 252,252,252); // gray
imagefill($finalImage, 0, 0, $backgroundColor);
// New sizes calculation
$percent = 0.5;
$new_width = $w * $percent;
$new_height = $h * $percent;
// First image add
imagecopyresampled($finalImage, $image, $new_width, $new_height, 0, 0,
$new_width, $new_height, $w, $h);
// Second image rotate with transparant bg
$transparency = imagecolorallocatealpha( $image,0,0,0,127 );
$rotatedImage = imagerotate( $image, -60, $transparency, 1);
imagealphablending( $rotatedImage, false );
imagesavealpha( $rotatedImage, true );
// Getting new rotated image sizes to avoid cutting border
$rw = imagesx($rotatedImage);
$rh = imagesy($rotatedImage);
// Second rotated image add with bigger space
imagecopyresampled($finalImage, $rotatedImage, 0, 0, 0, 0,
$rw*$percent, $rh*$percent, $rw, $rh);
// Outputing png image
header( 'Content-Type: image/png' );
imagepng( $finalImage );
change this line:
$finala = imagerotate($finala, -60, imageColorAllocateAlpha($finala, 0, 0, 0, 127));
to
$finala = imagerotate($finala, -60, imageColorAllocateAlpha($finala, 250, 252, 252, 127));
the black board should gone!

imagecopyresampled not working not sure why

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);

PHP Resize image and save Transparent PNG

I've a png image uploading script in my website and i'm scaling down the large images to small size and i'm using this code but it loses the transparent background
$image = $tmp; // Uploaded Image
$maxImgWidth = 224;
$src = imagecreatefrompng($image);
list($width, $height) = getimagesize($image);
$newwidth = $maxImgWidth;
$newheight = ($height / $width) * $newwidth;
$newImage = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagesavealpha($image, true);
imagecopyresampled($newImage, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($newImage, "../thumb/$name_after-$code.$ext", 1);
imagedestroy($src);
imagedestroy($newImage);
This code makes the image without transparency :
And i want with transparent background like this one :
Sorry I have no experience in PHP but after I read this page, I think you may need add the transparent colour after you set the save alpha is true
imagesavealpha($image, true);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $color);
if still cannot try add this after imagecolorallocatealpha(),
imagecolortransparent($image, $color);

Transparent PNG Going Black

I have looked through so many other threads with people having trouble with a png image going black where it should be transparent and none of the solutions have worked for me.
Maybe I am going wrong else where in the code? Maybe imagealhpablending isnt supported by my web server?
Thanks for anyone that can help.
$photo = imagecreatefrompng( "{$thumb_folder}{$new_file_name}" );
$width = imagesx($photo);
$height = imagesy($photo);
$new_width = 32;
$new_height = floor($height / ($width / $new_width));
$temp_photo = imagecreatetruecolor($new_width, $new_height);
imagealphablending($temp_photo, false);
imagesavealpha($temp_photo, true);
$transparent = imagecolorallocatealpha($temp_photo, 255, 255, 255, 127);
imagefilledrectangle($temp_photo, 0, 0, $new_width, $new_height, $transparent);
imagecopyresampled($temp_photo, $photo, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($temp_photo, "{$thumb_folder}{$new_file_name}" );
The first colour allocated to an image is always the background colour anyway, so there is no need to draw that filled rectangle. Remove it from the code, and see what you get.

Categories