Resizing and cropping image with GD while retaining aspect ratio - php

I'm currently coding an uploader script based on Uploadify. Right now I resize the given image and watermark one of the sizes. It all works well, but I need the script to resize the height and then crop the width so that the aspect ratio does not get messed up.
This is my code so far:
if ($fileExtension == "jpg" ||
$fileExtension == "jpeg" ||
$fileExtension == "png" ||
$fileExtension == "gif"){
// GD variables:
list($width, $height, $type) = GetImageSize($uploadedFile['tmp_name']);
// Image sizes:
$bigImage = array(800, 453);
$mediumImage = array(410, 231);
$listImage = array(120, 68);
$thumbnail = array(90, 51);
$sourceAspect = $width / $height;
$bigAspect = $bigImage[0] / $bigImage[1];
$mediumAspect = $mediumImage[0] / $mediumImage[1];
$listAspect = $listImage[0] / $listImage[1];
$thumbnailAspect = $thumbnail[0] / $thumbnail[1];
// Image is PNG:
if ($type == IMAGETYPE_PNG){
$image = imagecreatefrompng($uploadedFile['tmp_name']);
$valid = true;
}
// Image is JPEG:
else if ($type == IMAGETYPE_JPEG){
$image = imagecreatefromjpeg($uploadedFile['tmp_name']);
$valid = true;
}
// Image is GIF:
else if ($type == IMAGETYPE_GIF){
$image = imagecreatefromgif($uploadedFile['tmp_name']);
$valid = true;
}
// Format not allowed:
else {
$valid = false;
}
// Start creating images:
if ($valid){
// Get size:
$imageSize = getimagesize($uploadedFile['tmp_name']);
// Generate canvas:
$bCanvas = imagecreatetruecolor($bigImage[0], $bigImage[1]);
$mCanvas = imagecreatetruecolor($mediumImage[0], $mediumImage[1]);
$lCanvas = imagecreatetruecolor($listImage[0], $listImage[1]);
$tCanvas = imagecreatetruecolor($thumbnail[0], $thumbnail[1]);
// Copy content:
imagecopyresampled($bCanvas, $image, 0, 0, 0, 0, ($bigImage[0] * $sourceAspect), ($bigImage[1] / $sourceAspect), $imageSize[0], $imageSize[1]);
imagecopyresampled($mCanvas, $image, 0, 0, 0, 0, $mediumImage[0], $mediumImage[1], $imageSize[0], $imageSize[1]);
imagecopyresampled($lCanvas, $image, 0, 0, 0, 0, $listImage[0], $listImage[1], $imageSize[0], $imageSize[1]);
imagecopyresampled($tCanvas, $image, 0, 0, 0, 0, $thumbnail[0], $thumbnail[1], $imageSize[0], $imageSize[1]);
// Save images:
$saveB = imagejpeg($bCanvas, $targetFile.'_big.jpg', 90);
$saveM = imagejpeg($mCanvas, $targetFile.'_medium.jpg', 90);
$saveT = imagejpeg($lCanvas, $targetFile.'_list.jpg', 90);
$saveT = imagejpeg($tCanvas, $targetFile.'_thumb.jpg', 90);
// Destroy images:
imagedestroy($image);
imagedestroy($bCanvas);
imagedestroy($mCanvas);
imagedestroy($lCanvas);
imagedestroy($tCanvas);
// Watermark images:
$mark = imagecreatefrompng("logo.png");
list($mwidth, $mheight) = getimagesize("logo.png");
$img = imagecreatefromjpeg($targetFile.'_big.jpg');
list($bwidth, $bheight) = getimagesize($targetFile.'_big.jpg');
imagecopy($img, $mark, $bwidth-$mwidth-25, $bheight-$mheight-25, 0, 0, $mwidth, $mheight);
imagejpeg($img, $targetFile.'_big.jpg', 100);
imagedestroy($img);
} else {
echo "0";
}
} else {
move_uploaded_file($tempFile,$targetFile.'.'.$fileExtension);
}
I would be really happy if someone could help me solve this. I've been trying several methods but none of them seemed to work properly. As you can see in the top I have already defined the canvas sizes that I want to use in the variables "bigImage", "mediumImage", "listImage" and "thumbnail".
Thanks in advance! //
Jonathan

There is more than one way to resize an image. I'll spell them out for you:
Stretch to fit -- the image is resized to the desired size ignoring aspect ratio
Scale to fit -- the image is resized so that one dimension (width or height) has the desired size while the other is same or shorter while maintaining aspect ratio (one extra step may be required to fill the shorter side with solid color)
Crop to fit -- the image is resized so that one dimension (width or height) has the desired size while the other is same or longer while maintaining aspect ratio (one extra step is required to trim the outside region)
PS: both articles were written by me.

Related

how to cut a picture (not cropping) with php

I want to cut out part of the photo without stretch it.
Something like the photo I posted, cut out the red part and get photo number 2
With a width of 150px and height of 100px and cuting from top left of photo
enter image description here
I tried to do it with this code but it didn't work.
This codes separates part of the image, but does not do so from the top left of the image.
function resizejpeg($dir, $newdir, $img, $max_w, $max_h, $th_w, $th_h)
{
// set destination directory
if (!$newdir) $newdir = $dir;
// get original images width and height
list($or_w, $or_h, $or_t) = getimagesize($dir.$img);
// make sure image is a jpeg
if ($or_t == 2) {
// obtain the image's ratio
$ratio = ($or_h / $or_w);
// original image
$or_image = imagecreatefromjpeg($dir.$img);
// resize image?
if ($or_w > $max_w || $or_h > $max_h) {
// resize by height, then width (height dominant)
if ($max_h < $max_w) {
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}
// resize by width, then height (width dominant)
else {
$rs_w = $max_w;
$rs_h = $ratio * $rs_w;
}
// copy old image to new image
$rs_image = imagecreatetruecolor($rs_w, $rs_h);
imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h);
}
// image requires no resizing
else {
$rs_w = $or_w;
$rs_h = $or_h;
$rs_image = $or_image;
}
// generate resized image
imagejpeg($rs_image, $newdir.$img, 100);
$th_image = imagecreatetruecolor($th_w, $th_h);
// cut out a rectangle from the resized image and store in thumbnail
$new_w = (($rs_w / 2) - ($th_w / 2));
$new_h = (($rs_h / 2) - ($th_h / 2));
imagecopyresized($th_image, $rs_image, 0, 0, $new_w, $new_h, $rs_w, $rs_h, $rs_w, $rs_h);
// generate thumbnail
imagejpeg($th_image, $newdir.'thumb_'.$img, 100);
return true;
}
// Image type was not jpeg!
else {
return false;
}
}
$dir = './';
$img = '1.jpg';
$size = getimagesize($img);
$width = $size[0];
$height = $size[1];
resizejpeg($dir, '', $img, $width, $height, 150, 100);
I didn't understand correctly what you mean, but based on your description you are trying to get the image no 2 which means you are trying to crop an image. If that your mean, maybe this code will help
function crop($image_path, $output_path, $x, $y, $width, $height) {
// load image
$image = imagecreatefromjpeg($image_path);
// crop the image
$cropped_image = imagecrop($image, [
'x' => $x,
'y' => $y,
'width' => $width,
'height' => $height
]
);
// save it
imagejpeg($cropped_image, $output_path);
}
you can use it like this
// input image path
$image = "img.jpg";
// output image path
$output = "crop_img.jpg";
// crop it from (0,0)
crop($image, $output, 0, 0, 150, 100);

In PHP, how do you add whitespace around a non-square photo, so that it is always 200x200 pixels?

This script below works fine to handle an uploaded image and resize it so that the max height or width (whichever side is longer) is 200px. So it could be 200x200 if it's perfect square image, or 200x140, or 140x200, etc.
if(isset($_FILES['image'])) {
$img = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
// get uploaded file's extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
//checking if image exists for this pool and removing if so, before adding new image in its place
if(file_exists("uploads/".$poolid.".png")) {
unlink("uploads/".$poolid.".png");
}
// checks valid format
if(in_array($ext, $valid_extensions)) {
//re-size the image and make it a PNG before sending to server
$final_image = $poolid . ".png";
$path = "uploads/".strtolower($final_image);
$size = getimagesize($tmp);
$ratio = $size[0]/$size[1]; // width/height
if( $ratio > 1) {
$width = 200;
$height = 200/$ratio;
}
else {
$width = 200*$ratio;
$height = 200;
}
$src = imagecreatefromstring(file_get_contents($tmp));
$dst = imagecreatetruecolor($width,$height);
imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
imagedestroy($src);
imagepng($dst, $path); // adjust format as needed
imagedestroy($dst);
$_SESSION['image_uploaded']="yes";
echo $path ."?".rand(1,32000);
} else {
echo 'invalid file';
}
}
Now, Facebook sharing using OpenGraph requires an image to be at least 200x200. So a 140x200 image wouldn't work with their sharing functionality.
I don't love non-square images anyway, so I would like to take the image and if it's not already a square, I'd like to add whitespace to the sides (or on the top/bottom) and save it as a perfect 200x200 square every single time.
I tried this below, but it's not working (no image gets created at all). What is wrong with what I tried to do? This doesn't seem overly complicated but clearly I'm missing something.
if(isset($_FILES['image'])) {
$img = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
// get uploaded file's extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
//checking if image exists for this pool and removing if so, before adding new image in its place
if(file_exists("uploads/".$poolid.".png")) {
unlink("uploads/".$poolid.".png");
}
// checks valid format
if(in_array($ext, $valid_extensions)) {
//re-size the image and make it a PNG before sending to server
$final_image = $poolid . ".png";
$path = "uploads/".strtolower($final_image);
$size = getimagesize($tmp);
$ratio = $size[0]/$size[1]; // width/height
if( $ratio > 1) {
$width = 200;
$height = 200/$ratio;
}
else {
$width = 200*$ratio;
$height = 200;
}
$src = imagecreatefromstring(file_get_contents($tmp));
$dst = imagecreatetruecolor($width,$height);
$orig_img=imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
imagedestroy($src);
// create new image and fill with background colour
$new_img = imagecreatetruecolor($output_w, $output_h);
$bgcolor = imagecolorallocate($new_img, 255, 0, 0); // red
imagefill($new_img, 0, 0, $bgcolor); // fill background colour
// copy and resize original image into center of new image
$final_img=imagecopyresampled($new_img, $orig_img, 0, 0, 0, 0, 200, 200, $width, $height);
imagepng($final_img, $path); // adjust format as needed
imagedestroy($dst);
$_SESSION['image_uploaded']="yes";
echo $path ."?".rand(1,32000);
} else {
echo 'invalid file';
}
}
You don't need a temporary intermediate image. You can paste the resampled source image right into the destination image after you fill it with background. See here:
$src = imagecreatefromstring(file_get_contents($tmp));
// Create new image and fill it with background color
$dst = imagecreatetruecolor($output_w,$output_h);
$bgcolor = imagecolorallocate($dst, 255, 0, 0);
imagefill($dst, 0, 0, $bgcolor);
// Copy resampled src image into dst
if ($ratio > 1)
imagecopyresampled($dst, $src, 0, ($output_h - $height) / 2, 0, 0, $width, $height, $size[0], $size[1]);
else
imagecopyresampled($dst, $src, ($output_w - $width) / 2, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagepng($dst, $path); // adjust format as needed
imagedestroy($src);
imagedestroy($dst);

Transparency in GD PHP library

I'm attempting to allow transparency when compressing a file upload.
I created a function in which it will change the image to a jpeg (no matter what is uploaded), however, my client wants the images to have transparency. For ease, what I will do is if the image is a PNG, I will save it out as a PNG with transparency enabled, otherwise, I will just save it as a "flat" jPEG.
Here is the function I created.
function resizeImage($image, $resizeHeight = null, $resizeWidth = null, $quality = 80)
{
$img = getimagesize($image);
$ext = explode(".",$image);
list($width, $height, $type, $attr) = $img;
if($resizeHeight == null)
{
//Getting the new height by dividing the image width by the height and multiplying by the new width.
$resizeHeight = ($height/$width)*$resizeWidth;
}
if($resizeWidth == null)
{
//Getting the new width by dividing the image height by the width and multiplying by the new height.
$resizeWidth = ($width/$height)*$resizeHeight;
}
$name = uniqid().time().".".$ext[count($ext)-1];
if($img['mime'] == "image/jpeg")
{
$image = imagecreatefromjpeg($image);
}
else if($img['mime'] == "image/png")
{
$image = imagecreatefrompng($image);
}
$destImg = imagecreatetruecolor($resizeWidth, $resizeHeight);
if(imagecopyresampled($destImg, $image, 0, 0, 0, 0, $resizeWidth, $resizeHeight, $width, $height))
{
if(imagejpeg($destImg, DIRUPLOADFOLDER.$name, $quality))
{
return $name;
}
}
}
Needless to say, this function only saves out as a JPEG. I tried replacing imagejpeg with imagepng but that didn't work (like it didn't at all create an image). What do I need to do in order to make sure that the saved out PNG will also save out with the original's transparency?

Manipulating an image in PHP results in striped pattern

Edit: Just ended up using Image Magick and that fixed it.
Long story short, I'm attempting to pull some basic information about a series of schools from Wikipedia using a combination of PHP and jQuery. Part of that information is the school's emblem or logo, which is easy enough to find in the elements list.
The problem lies in trying to do some tweaking on the image in PHP. I know the image exists at the target URL (which is on a different domain, if that helps any) and that it's the one I want, but certain images come out looking like this:
This is the original image:
Others, of all file types, come out perfectly fine.
The code for that part is as follows:
$ext = end(explode('.', $image));
if($ext == 'png') {
$img = imagecreatefrompng($image);
}
else if($ext == 'jpeg' || $ext == 'jpg') {
$img = imagecreatefromjpeg($image);
}
else if($ext == 'gif') {
$img = imagecreatefromgif($image);
}
else $img = false;
if($img) {
$raw_x = imagesx($img);
$raw_y = imagesy($img);
if($raw_x > $raw_y && $raw_x > 500)
{
$y = (500 / $raw_x) * $raw_y;
$tmp_img = imagecreatetruecolor(500, $y);
$white = imagecolorallocate($tmp_img, 255, 255, 255);
imagefill($tmp_img, 0, 0, $white);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, 500, $y, $raw_x, $raw_y);
$img = $tmp_img;
}
else if($raw_y > 500)
{
$x = (500 / $raw_y) * $raw_x;
$tmp_img = imagecreatetruecolor($x, 500);
$white = imagecolorallocate($tmp_img, 255, 255, 255);
imagefill($tmp_img, 0, 0, $white);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $x, 500, $raw_x, $raw_y);
$img = $tmp_img;
}
if(!file_exists("../images/schools/" . $id)) mkdir("../images/schools/" . $id, 0755, true);
imagejpeg($img, "../images/schools/" . $id . "/photo.jpg", 100);
}
I've been at this for a couple days and I can't figure out what's wrong, I'm hoping a fresh set of eyes might be able to see something I don't
My guess is that your conditional logic around raw image size is causing you problems. You are not handling the case where you image equal in width and height and you are not handling cases where the images are less than 500 pixels (not sure if this is intentional, but could lead to problems with HTML layout if you specify specific heights/widths for your img elements). You should do all your math to calculate expected scaled image size up front and then do image creation/resizing in one place (not in if conditionals).
I am assuming you want to always scale your image to be either 500px in height (if image is portrait) or 500px in width if image is landscape. You would calculate expected dimensions as follows:
$target_width = 500;
$target_height = 500;
if($raw_x >= $raw_y) { // set scaling factor based on x dimension
$scaling_factor = 500.0 / $raw_x;
$target_height = intval(500 * $scaling_factor);
} else { // set scaling factor based on y dimension
$scaling_factor = 500.0 / $raw_y;
$target_width = intval(500 * $scaling_factor)
}
And then only have one code block for setting new temp image and resizing original into it:
$tmp_img = imagecreatetruecolor($target_width, $target_height);
$white = imagecolorallocate($tmp_img, 255, 255, 255);
imagefill($tmp_img, 0, 0, $white);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $target_width, $target_height, $raw_x, $raw_y);
$img = $tmp_img;

Image cropping and thumb creation

I need to help a friend to make his own artistic gallery.
I already have all done, but I need a tool/plugin/script to make him independent from me in uploading his own images. My gallery needs two images: a cropped one of a certain proportion (so i need him to crop by himself in an uploading page) and a thumb one (I want this be done automatically).
Do you know an easy way to do this? What would you do?
Thanks.
Personally i use this in all of my projects - http://www.verot.net/php_class_upload.htm
Works perfectly with upload files and with files that's already on the system.
Can do many things convert, resize and work on uploaded images in many ways, apply effects, add labels, watermarks and reflections and other image editing features.
Easy to work with.
If you're not going to be having heavy traffic to start - look at http://phpthumb.sourceforge.net/ which can create your resized images on the fly.
You need only GD library, function imagecopyresampled will suit you. PHP manual has really good example code for thumbnail creation http://php.net/manual/en/function.imagecopyresampled.php
You will need just to create exceptions for different file formats
function create_jpeg_thumbnail($thumbImageName,$imgSrc,$thumbDirectory,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource.
$thumbDirectory = trim($thumbDirectory);
$imageSourceExploded = explode('/', $imgSrc);
$imageName = $imageSourceExploded[count($imageSourceExploded)-1];
$imageDirectory = str_replace($imageName, '', $imgSrc);
$filetype = explode('.',$imageName);
$filetype = strtolower($filetype[count($filetype)-1]);
//getting the image dimensions
list($width_orig, $height_orig) = getimagesize($imgSrc);
//$myImage = imagecreatefromjpeg($imgSrc);
if ($filetype == 'jpg') {
$myImage = imagecreatefromjpeg("$imageDirectory/$imageName");
} else
if ($filetype == 'jpeg') {
$myImage = imagecreatefromjpeg("$imageDirectory/$imageName");
} else
if ($filetype == 'png') {
$myImage = imagecreatefrompng("$imageDirectory/$imageName");
} else
if ($filetype == 'gif') {
$myImage = imagecreatefromgif("$imageDirectory/$imageName");
}
$ratio_orig = $width_orig/$height_orig;
if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
$new_height = $thumbnail_width/$ratio_orig;
$new_width = $thumbnail_width;
} else {
$new_width = $thumbnail_height*$ratio_orig;
$new_height = $thumbnail_height;
}
$x_mid = $new_width/2; //horizontal middle
$y_mid = $new_height/2; //vertical middle
$process = imagecreatetruecolor(round($new_width), round($new_height));
imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($thumb, $process, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
//$thumbImageName = 'thumb_'.get_random_no().'.jpeg';
$destination = $thumbDirectory=='' ? $thumbImageName : $thumbDirectory."/".$thumbImageName;
imagejpeg($thumb, $destination, 100);
return $thumbImageName;
}

Categories