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.
Related
I have to create dynamic student card image. add put this image object in student profile photo. But, student image color are changed.
How to put student profile photo with original color?
Here is my code:
header("Content-Type: image/jpeg");
$im = #imagecreate(602, 980)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$card_header = imagecreatefromjpeg('img/card/card-header.jpg');
imagecopy($im, $card_header, 0, 0, 0, 0, 602, 253);
$card_footer = imagecreatefromjpeg('img/card/card-footer.jpg');
imagecopy($im, $card_footer, 0, 834, 0, 0, 602, 146);
$student_photo = 'img/card/girls-profile.jpg'; //imagecreatefromjpeg($studentlist[0]->getCardPhoto());
// Get new sizes
list($width, $height) = getimagesize($student_photo);
$newwidth = 180;
$newheight = 220;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($student_photo);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopy($im, $thumb, 220, 220, 0, 0, $newwidth, $newheight);
imagejpeg($im, "uploads/card/test.jpeg");
imagedestroy($im);
Header Img:
Footer Img:
Profile Img:
Here is my output image:
Main problem is that your $im needs to be a true color image as well.
Secondly, you have to actually fill your background.
You can also skip creating $thumb and copyresized directly into your $im.
Heres a working verison (i changed your paths to test it on my machine)
<?php
header('Content-Type: image/jpeg');
$im = #imagecreatetruecolor(602, 980) // you want to create a truecolorimage here
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $background_color); // you have to actually use the allocated background color
$card_header = imagecreatefromjpeg('card-header.jpg');
imagecopy($im, $card_header, 0, 0, 0, 0, 602, 253);
$card_footer = imagecreatefromjpeg('card-footer.jpg');
imagecopy($im, $card_footer, 0, 834, 0, 0, 602, 146);
$student_photo = 'girls-profile.jpg';
// Get new sizes
list($width, $height) = getimagesize($student_photo);
$newwidth = 180;
$newheight = 220;
// Load
//$thumb = imagecreatetruecolor($newwidth, $newheight); // you can skip allocating extra memory for a intermediate thumb
$source = imagecreatefromjpeg($student_photo);
// Resize
imagecopyresized($im, $source, 220, 220, 0, 0, $newwidth, $newheight, $width, $height); // and copy the thumb directly
imagejpeg($im);
imagedestroy($im);
// you should also destroy the other images
imagedestroy($card_header);
imagedestroy($card_footer);
imagedestroy($source);
Keep in mind that your profile picture current gets distorted though, you may wan't to either make sure the profile pictures always have the correct aspect ratio or you may want to crop the image. See here for more details: PHP crop image to fix width and height without losing dimension ratio
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);
I'm trying to redesign my site so that my original square, tile-based rendering of images can be more of a cutout of the image... to get rid of that grid pattern.
Here's how it looked originally...
Here's a rough mock-up of what I'm going for:
So I resaved an image thumbnail with a transparent background... I just want the dog to show, and the square is transparent which will show the site's background underneath.
Yet when I render it on the page, it has this black background.
I've checked my CSS to see if there is some sort of img class, or class for the rendered comics... or even the bootstrap to see where there may be a background-color being assigned to black (and also searched for hex code 000000), but didn't find one...
Then I found that it had to do with the way my thumbnailing script was resampling the png... Since I'm getting a black background for the supposedly transparent image, I blame imagecreatetruecolor() which returns an image identifier representing a black image of the specified size..
I tried following Cheekysoft's question here about preserving transparency after resampling... but it didn't work... with his two main points being:
imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );
I found if I put $img = imagecreatefrompng($image_file); before I resize/resample it, it displays transparently... which is what I want... but not after I resample it.
Thumbnailer.php code:
<?php
#Appreciation goes to digifuzz (http://www.digifuzz.net) for help on this
$image_file = $_GET['img']; //takes in full path of image
$MAX_WIDTH = $_GET['mw'];
$MAX_HEIGHT = $_GET['mh'];
global $img;
//Check for image
if(!$image_file || $image_file == "") {
die("NO FILE.");
}
//If no max width, set one
if(!$MAX_WIDTH || $MAX_WIDTH == "") {
$MAX_WIDTH="100";
}
//if no max height, set one
if(!$MAX_HEIGHT || $MAX_HEIGHT == "") {
$MAX_HEIGHT = "100";
}
$img = null;
//create image file from 'img' parameter string
if( preg_match('/\.jpg$/',$image_file) || preg_match('/\.jpeg$/',$image_file) ){
$img = imagecreatefromjpeg($image_file);
} else {
$img = imagecreatefrompng($image_file);
}
//if image successfully loaded...
if($img) {
//get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
//takes min value of these two
$scale = min($MAX_WIDTH/$width, $MAX_HEIGHT/$height);
//if desired new image size is less than original, output new image
if($scale < 1) {
$new_width = floor($scale * $width);
$new_height = floor($scale * $height);
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//copy and resize old image to new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//replace actual image with new image
$img = $tmp_img;
}
}
//set the content type header
header('Content-Type: image/png', true);
imagealphablending( $img, false );
imagesavealpha( $img, true );
imagepng($img);
imagedestroy($img);
?>
Can anyone help?
Thanks!
You need to call imagealphablending() on your destination image BEFORE you resample/resize the image:
//copy and resize old image to new image
imagealphablending($tmp_img, false);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// ...
You placed alphablending and traparent code well below after creating image. Just place them before the your createresample line and transparent image with image that created by imagecreatefrompng.
imagealphablending( $tmp_img, false );
imagesavealpha( $tmp_img, true );
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Or you can Try following
$img = ImageCreateFromPNG($image_file);
$tmp_img = imagecreatetruecolor($new_width,$new_height);
imagecolortransparent($tmp_img, imagecolorallocate($tmp_img, 0, 0, 0));
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
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.
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.