Image resizing and cropping with Php - php

I'm using a php script to upload and resize an image, pretty simple:
if($_SERVER["REQUEST_METHOD"] == "POST") {
$image = $_FILES["image_upload"];
$uploadedfile = $image['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['image_upload']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$error_txt = 'Immagine incoretta';
$errors=1;
} else {
$size=filesize($uploadedfile);
if ($size > MAX_SIZE*1024) {
$error_txt = "Immagine troppo grande";
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ) {
$uploadedfile = $uploadedfile;
$src = imagecreatefromjpeg($uploadedfile);
} else if($extension=="png") {
$uploadedfile = $uploadedfile;
$src = imagecreatefrompng($uploadedfile);
} else {
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=500;
$newheight=375;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "images/". generateRandomString(5) . $image['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
}
}
I want to got a bit further, right now im just resizing the image no matter the proportions, the think is, i want to resize it to a fixed with and height without losing the original proportion, and this of course is achieved through the cropping+resize of the original image.
I have no idea how to do this using my actual imagecreatetruecolor and imagecopyresampled functions, and looking to the php manual seems is not very easy.
There is a very good library im trying to integrate to my code, the use its as simple as mysite.com/lib/timthumb.php?src=castle1.jpg&h=180&w=120 but i dont know how to integrate that with my actual code.
So, what do you suggest?

Please forgive me if there are any typos or anything in the following code. I haven't tested it. What I've done here is calculate whether the height or the width is the proportion that's too long. Then adjust the source dimension to match the final image dimension. Also, adjust the center of the side that we shrunk so the cropped image is centered.
$newwidth = 500;
$newheight = 375;
$tmp = imagecreatetruecolor($newwidth, $newheight);
$widthProportion = $width / $newwidth;
$heightProportion = $height / $newheight;
if ($widthProportion > $heightProportion) {
// width proportion is greater than height proportion
// figure out adjustment we need to make to width
$widthAdjustment = ($width * ($widthProportion - $heightProportion));
// Shrink width to proper proportion
$width = $width - $widthAdjustment;
$x = 0; // No adjusting height position
$y = $y + ($widthAdjustment / 2); // Center the adjustment
} else {
// height proportion is greater than width proportion
// figure out adjustment we need to make to width
$heightAdjustment = ($height * ($heightProportion - $widthProportion));
// Shrink height to proper proportion
$height = $height - $heightAdjustment;
$x = $x + ($heightAdjustment / 2); // Center the ajustment
$y = 0; // No adjusting width position
}
imagecopyresampled($tmp, $src, 0, 0, $x, $y, $newwidth, $newheight, $width, $height);
So basically with the $width and $height variables you are specifying how much of the picture you want (cropping). and with the $x, $y we're saying where we want to crop the picture. The rest is just the standard resizing to fit the full new image.
I hope that helps!

Related

PHP - Thumbnail created from high definition uploaded image returning blurry

I've got an issue which potentially could be a really easy fix, but I'm struggling to figure it out. So here's the issue:
When I upload a high definition photo in my upload script, the thumbnail returns blurry. For example:
The original photo below of our future supreme leader Elon musk looks like this:
https://i.stack.imgur.com/T91t4.jpg
The thumbnail returns something like this:
https://i.stack.imgur.com/9Vb6B.jpg
As you can see, the thumbnail is so blurry where I wouldn't wanna display it on a webpage. I'll leave a snippet of the PHP code to create the thumbnail:
mkdir($_SERVER["DOCUMENT_ROOT"]."/threads/$id/thumbnails/", 0755);
$filepath = "/threads/$id/".$fileName;
$filepath_thumb = "/threads/$id/thumbnails/".$fileName;
if($fileType == "image/jpeg")
{
$imagecreate = "imagecreatefromjpeg";
$imageformat = "imagejpeg";
}
if($fileType == "image/png")
{
$imagecreate = "imagecreatefrompng";
$imageformat = "imagepng";
}
if($fileType == "image/gif")
{
$imagecreate= "imagecreatefromgif";
$imageformat = "imagegif";
}
// Adjust size of thumbnail
$maxwidth = 250;
$maxheight = 250;
if ($fileheight > $filewidth)
{
$ratio = $maxheight / $fileheight;
$newheight = $maxheight;
$newwidth = $filewidth * $ratio;
}
else
{
$ratio = $maxwidth / $filewidth;
$newwidth = $maxwidth;
$newheight = $fileheight * $ratio;
}
$image = $imagecreate($_SERVER["DOCUMENT_ROOT"]."$filepath");
$image_p = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $filewidth, $fileheight);
$imageformat($image_p, $_SERVER["DOCUMENT_ROOT"]."$filepath_thumb"); // Thumbnail Folder
If you don't pass quality parameters to imagejpeg or imagepng, they default to sub-optimal quality settings. So you need to know if you are you are processing a JPG or PNG, and handle accordingly.
$outputPath = $_SERVER["DOCUMENT_ROOT"]."$filepath_thumb";
if($fileType == "image/jpeg")
{
$imageformat($image_p, $outputPath, 100);
}
elseif($fileType == "image/png")
{
$imageformat($image_p, $outputPath, 9);
}
else
{
$imageformat($image_p, $outputPath);
}
You also may have better results with imagecopyresampled rather than imagecopyresized. I have always use imagecopyresampled for thumbnails, I seem to recall that it produces better results when reducing size.

Php Resizing image increase image size

I'm creating thumnails of uploaded file.
if image width and height are greater than 200 than then i re size them to 200px.
Here is code i used to do that:
if (file_exists($old_file)) {
$path_parts = pathinfo($old_file);
$extension = $path_parts['extension'];
$filename_path = $filepath . $filename;
$destination_path = $filename_path;
if (strtolower($extension) == "jpg" || strtolower($extension) == "jpeg") {
$uploadedfile = $old_file;
$src = imagecreatefromjpeg($uploadedfile);
} else if (strtolower($extension) == "png") {
$uploadedfile = $old_file;
$src = imagecreatefrompng($uploadedfile);
} else {
$uploadedfile = $old_file;
$src = imagecreatefromgif($uploadedfile);
}
list($width, $height) = getimagesize($uploadedfile);
$newwidth = $Size['width'];
$newheight = $Size['height'];
if ($width <= $newwidth && $height <= $newheight) {
$newwidth = $width;
$newheight = $height;
$tmp = imagecreatetruecolor($width, $height);
} else {
if ($width > $height) {
$newheight = ($height / $width) * $newwidth;
$tmp = imagecreatetruecolor($newwidth, $newheight);
} else {
$newwidth = ($width / $height) * $newheight;
$tmp = imagecreatetruecolor($newwidth, $newheight);
}
}
if ((strtolower($extension) == "png") OR (strtolower($extension) == "gif")) {
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
}
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
if (strtolower($extension) == "jpg" || strtolower($extension) == "jpeg") {
imagejpeg($tmp, $destination_path, 100);
} elseif (strtolower($extension) == "png") {
imagepng($tmp, $destination_path, 5);
} else {
imagegif($tmp, $destination_path);
}
chmod($destination_path, 0777);
imagedestroy($src);
imagedestroy($tmp);
ob_flush();
flush();
ob_end_flush();
return true;
} else {
return false;
}
it re size the large images to 200px by 200px but image size is increased in (byte and kb etc increased).
I tried uploading 8kb png file, and new thumbnail file size was 28kb?
Tried googling but didn't find anything helpful
Thanks.
Your source image is compressed, after parsing it you get a true color image, which is uncompressed. Then you save it with a compression level of 5 (in the case of PNG), which is pretty low compression, thus a higher filesize.
Try a higher compression, like 9, for example. Also try adding a combination of filters to decrease filesize (http://us3.php.net/manual/en/image.constants.php look for PNG_FILTER_*).
See: http://us3.php.net/manual/en/function.imagepng.php
http://en.wikipedia.org/wiki/Portable_Network_Graphics#Compression
http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_size_factors
The GD library doesn't appear to provide any interfaces to let you in on low-level PNG data, but you can theoretically find out the source compression level and filters, by using other bindings or trying to read it manually.
http://www.libpng.org/pub/png/spec/1.2/PNG-Compression.html
http://www.libpng.org/pub/png/spec/1.2/PNG-Filters.html
The same may happen with JPG and GIF.

Codeigniter Image Library Cropping an Image with 2 sets of coordinates to make square anywhere?

Spending time reading through the docs, and searching for examples. I understand cropping an image from top 0, and left 0 is pretty straight forward. However. I would like to pass 2 sets of coordinates, a starting point and an ending point. Four Points, a Square that is defined anywhere. However from the examples I am finding, and from what I gather the rendition is not going to let me do this. codeigniter so I am seeking confirmation on this thought, is it true that I can only provide end points from 0, and it crops a square based on said end points or can I actually specify an x start, an x end, and similar for y?
Or is there some other technique I can use within codeigniter that will allow me to pass for coordinates for starting and ending points?
You can use for example of class of defined cropping image and call it a class via library or helper in a codeigniter:
For example call it:
LibraryCropImage::ResizedThumbnail($profile_thumbsize_x, $profile_thumbsize_x, $path, $filename, $path_to, $filename_to.'_sized', $quality, $fileextension);
LibraryCropImage::CroppedThumbnail($profile_thumbsize_x, $profile_thumbsize_x, $path, $filename, $path_to, $filename_to.'_cropped', $quality, $fileextension);
abstract class LibraryCropImage {
function ResizedThumbnail($thumbnail_width, $thumbnail_height, $path, $filename, $path_to, $filename_to, $quality, $ext)
{
list($width, $height) = getimagesize($path.$filename);
$new_width = $thumbnail_width;
$new_height = $thumbnail_height;
if ($ext == "jpg" || $ext == "jpeg")
{
$thumb = #imagecreatefromjpeg($path.$filename);
}
else if ($ext == "gif")
{
$thumb = #imagecreatefromgif($path.$filename);
}
else if ($ext == "png")
{
$thumb = #imagecreatefrompng($path.$filename);
}
$thumbp = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($thumbp, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
ob_start();
if ($ext == "jpg" || $ext == "jpeg")
{
imagejpeg($thumbp, null, $quality);
}
else if ($ext == "gif")
{
imagegif($thumbp);
}
else if ($ext == "png")
{
imagepng($thumbp, null);
}
$i = ob_get_clean();
$fp = fopen($path_to.$filename_to.'.'.$ext, 'w');
fwrite ($fp, $i);
fclose ($fp);
imagedestroy($thumbp);
}
function CroppedThumbnail($thumbnail_width, $thumbnail_height, $path, $filename, $path_to, $filename_to, $quality, $ext)
{
list($width, $height) = getimagesize($path.$filename);
$new_width = $thumbnail_width;
$new_height = $thumbnail_height;
if ($ext == "jpg" || $ext == "jpeg")
{
$image = #imagecreatefromjpeg($path.$filename);
}
else if ($ext == "gif")
{
$image = #imagecreatefromgif($path.$filename);
}
else if ($ext == "png")
{
$image = #imagecreatefrompng($path.$filename);
}
$filename = $path.$filename;
$thumb_width = $thumbnail_width;
$thumb_height = $thumbnail_height;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if($original_aspect >= $thumb_aspect) {
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
} else {
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
ob_start();
if ($ext == "jpg" || $ext == "jpeg")
{
imagejpeg($thumb, null, $quality);
}
else if ($ext == "gif")
{
imagegif($thumb);
}
else if ($ext == "png")
{
imagepng($thumb, null);
}
$i = ob_get_clean();
$fp = fopen($path_to.$filename_to.'.'.$ext, 'w');
fwrite ($fp, $i);
fclose ($fp);
imagedestroy($thumb);
}
}
Suggestions for you:
Use imagecopyresized()
Use exec() in conjunction with vcrop on Debian
Use Imagick::cropImage or exec() in conjunction with convert -crop
I answered this question here: how to crop image in codeigniter?
Basically, you provide two axes (x/y). But it will not crop the area from 0 to that axis as you think, it will actually return the part which is closer to the center. So for that reason it is possible to crop out an area between (x1,y1) and (x2,y2).

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

PHP: imagecopyresampled returns false and URLs

I'm trying to write a simple file uploader which can resize an image if needed and copy it into the server's file system. That works if I pass the file directly but I also want to be able to pass a URL via a GET parameter. And somehow imagecopyresampled seems to fail with the URL.
Here is my code:
if(isset($_FILES["file"]))
$fn = $_FILES['file']['tmp_name'];
else
$fn = urldecode($_GET['url']);
$fileMD5 = md5_file($fn);
$target = $basedir . $fileMD5 . ".png";
list($width, $height, $imgtype) = getimagesize($fn);
if ($imgtype == IMAGETYPE_PNG)
$img = imagecreatefrompng($fn);
else if ($imgtype == IMAGETYPE_JPEG)
$img = imagecreatefromjpeg($fn);
else if ($imgtype == IMAGETYPE_GIF)
$img = imagecreatefromgif($fn);
else if ($imgtype == IMAGETYPE_BMP)
$img = imagecreatefromwbmp($fn);
else {
echo "unsupported file format";
return;
}
// image resize
if($width > $maxwidth && $width >= $height) {
$newwidth = $maxwidth;
$newheight = ($height / $width) * $newwidth;
} else if($height > $maxheight) {
$newheight = $maxheight;
$newwidth = ($width / $height) * $newheight;
}
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($tmp, $target);
Everything seems to work fine until imagecopyresampled. I get the correct IMAGETYPE and also, something is created with imagecreatetruecolor. But imagecopyresampled returns false. I'm pretty confused since the script seems to be able to actually read the image and get it's first bits to determine the type of it.
Any suspections what's going wrong?
Thanks in advance.
Your resize section seems to leave out some situations that could lead to an undefined $newheight and $newwidth.
Are you sure about imagecreatetruecolor returning an image?

Categories