Image imagejpeg() returns nothing. CakePHP - php

I am modifieng this plug in to save a record of the path and make a thumbnail image and save that aswell http://bakery.cakephp.org/articles/srs2012/2012/03/12/ajaxmultiupload_plugin_for_cake_2_0_x_and_2_1
I modified the upload.php in the plugin like this:
function save($path, $folder, $filename) {
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
if ($realSize != $this->getSize()){
return false;
}
$width = 290;
$height = 146;
$target = fopen($path, "w");
//$this->Upload->setImage($this->Image);
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
$val = $this->resizeImage($filename, $filename, $folder, $folder.'/small/', $width, $height, 100);
$this->saveToDatabase(array('path' => $folder.$filename, 'thumb' => $folder.'/small/'.$filename));
return true;
}
This is the function i made to save:
function saveToDatabase($data){
$this->Image->save($data);
}
The following I added to create the thumbnail:
function resizeImage($src_img, $dst_img, $src_path2, $dst_path2, $dst_w, $dst_h, $dst_quality){
//Stop and giving an error if the file does not exists.
$src_path = 'img/';
$dst_path = 'img/';
$src_path .= $src_path2;
$dst_path .= $dst_path2;
if(file_exists($src_path . basename($src_img)) == false){
echo 0;
}
//Get variables for the function.
//complete path of the source image.
$src_cpl = $src_path . basename($src_img);
//return $src_cpl;
//complete path of the destination image.
$dst_cpl = $dst_path . basename($dst_img);
//extension excl "." of the source image, in lowercase.
$src_ext = strtolower(end(explode(".", $src_img)));
//width and height sizes of the source image.
list($src_w, $src_h) = getimagesize($src_cpl);
//get type of image.
//return 'IETS: '.$src_cpl.' :IETS';
$src_type = exif_imagetype($src_cpl);//
//Checking extension and imagetype of the source image and path.
if( ($src_ext =="jpg") && ($src_type =="2") ){
$src_img = imagecreatefromjpeg($src_cpl);
}else if( ($src_ext =="jpeg") && ($src_type =="2") ){
$src_img = imagecreatefromjpeg($src_cpl);
}else if( ($src_ext =="gif") && ($src_type =="1") ){
$src_img = imagecreatefromgif($src_cpl);
}else if( ($src_ext =="png") && ($src_type =="3") ){
$src_img = imagecreatefrompng($src_cpl);
}else{
die('<p>The file "'. $src_img . '" with the extension "' . $src_ext . '" and the imagetype "' . $src_type . '" is not a valid image. Please upload an image with the extension JPG, JPEG, PNG or GIF and has a valid image filetype.</p>');
}
//Get heights and width so the image keeps its ratio.
$x_ratio = $dst_w / $src_w;
$y_ratio = $dst_h / $src_h;
if( (($x_ratio > 1) || ($y_ratio > 1)) && ($x_ratio > $y_ratio) ){
//If one of the sizes of the image is smaller than the destination (normal: more height than width).
$dst_w = ceil($y_ratio * $src_w);
$dst_h = $dst_h;
}elseif( (($x_ratio > 1) || ($y_ratio > 1)) && ($y_ratio > $x_ratio) ){
//If one of the sizes of the image is smaller than the destination (landscape: more width than height).
$dst_w = $dst_w;
$dst_h = ceil($x_ratio * $src_h);
}elseif (($x_ratio * $src_h) < $dst_h){
//if the image is landscape (more width than height).
$dst_h = ceil($x_ratio * $src_h);
$dst_w = $dst_w;
}elseif (($x_ratio * $src_h) > $dst_h){
//if the image is normal (more height than width).
$dst_h = ceil($x_ratio * $src_h);
$dst_w = $dst_w;
}else{
//if the image is normal (more height than width).
$dst_w = ceil($y_ratio * $src_w);
$dst_h = $dst_h;
}
// Creating the resized image.
$dst_img=imagecreatetruecolor($dst_w,$dst_h);
$result = imagecopyresampled($dst_img,$src_img,0,0,0,0,$dst_w, $dst_h,$src_w,$src_h);
// Saving the resized image.
$result2 = imagejpeg($dst_img,$dst_cpl,$dst_quality);
return 'dfdfhdfhhgfddghhgffddghdghhdgghgfhdh: '.$result.'|||asdasda'.$result2;
// Cleaning the memory.
imagedestroy($src_img);
imagedestroy($dst_img);
}
enter code here
Now I already used this code on the same server and it worked perfectly fine. But in my return value i get the following:
[uploader] responseText = dfdfhdfhhgfddghhgffddghdghhdgghgfhdh: 1|||asdasda{"success":true}
The function imagejpeg does not return anything.
Now what could be wrong? I am only an intern and I can't figure out what is wrong.
Greetings,
Harm.
bump?

See http://php.net/manual/en/function.imagejpeg.php - I think, that you have problem in function imagejpeg - see parameter filename.
If there will be more problems, see http://www.php.net/manual/en/function.gd-info.php

Related

How to set aspect ratio in below code using PHP

I tried 2 implementations, shown below. Both are working well but when I tried to mix them it is not working anymore.
$output['status']=FALSE;
set_time_limit(0);
$allowedImageType = array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png" );
if ($_FILES['image_file_input']["error"] > 0) {
$output['error']= "File Error";
}
elseif (!in_array($_FILES['image_file_input']["type"], $allowedImageType)) {
$output['error']= "Invalid image format";
}
elseif (round($_FILES['image_file_input']["size"] / 1024) > 4096) {
$output['error']= "Maximum file upload size is exceeded";
} else {
$temp_path = $_FILES['image_file_input']['tmp_name'];
$file = pathinfo($_FILES['image_file_input']['name']);
$fileType = $file["extension"];
$photo_name = $productname.'-'.$member_id."_".time();
$fileName1 = $photo_name . '-125x125' . ".jpg";
$fileName2 = $photo_name . '-250x250' . ".jpg";
$fileName3 = $photo_name . '-500x500' . ".jpg";
$small_thumbnail_path = "uploads/large/";
createFolder($small_thumbnail_path);
$small_thumbnail = $small_thumbnail_path . $fileName1;
$medium_thumbnail_path = "uploads/large/";
createFolder($medium_thumbnail_path);
$medium_thumbnail = $medium_thumbnail_path . $fileName2;
$large_thumbnail_path = "uploads/large/";
createFolder($large_thumbnail_path);
$large_thumbnail = $large_thumbnail_path . $fileName3;
$thumb1 = createThumbnail($temp_path, $small_thumbnail,$fileType, 125, 125 );
$thumb2 = createThumbnail($temp_path, $medium_thumbnail, $fileType, 250, 250);
$thumb3 = createThumbnail($temp_path, $large_thumbnail,$fileType, 500, 500);
if($thumb1 && $thumb2 && $thumb3) {
$output['status']=TRUE;
$output['small']= $small_thumbnail;
$output['medium']= $medium_thumbnail;
$output['large']= $large_thumbnail;
}
}
echo json_encode($output);
Function File
function createFolder($path)
{
if (!file_exists($path)) {
mkdir($path, 0755, TRUE);
}
}
function createThumbnail($sourcePath, $targetPath, $file_type, $thumbWidth, $thumbHeight){
$source = imagecreatefromjpeg($sourcePath);
$width = imagesx($source);
$height = imagesy($source);
$tnumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($tnumbImage, $source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
if (imagejpeg($tnumbImage, $targetPath, 90)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
} else {
return FALSE;
}
}
Aspect ratio code, which is another one I tried to mix this code. But I was unsuccessful
$fn = $_FILES['image']['tmp_name'];
$size = getimagesize($fn);
$ratio = $size[0]/$size[1]; // width/height
$photo_name = $productname.'-'.$member_id."_".time();
{
if( $ratio > 1) {
$width1 = 500;
$height1 = 500/$ratio;
}
else {
$width1 = 500*$ratio;
$height1 = 500;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width1,$height1);
$fileName3 = $photo_name . '-500x500' . ".jpg";
imagecopyresampled($dst,$src,0,0,0,0,$width1,$height1,$size[0],$size[1]);
imagedestroy($src);
imagepng($dst,$fileName3); // adjust format as needed
imagedestroy($dst);
if( $ratio > 1) {
$width2 = 250;
$height2 = 250/$ratio;
}
else {
$width2 = 250*$ratio;
$height2 = 250;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width2,$height2);
$fileName2 = $photo_name . '-250x250' . ".jpg";
imagecopyresampled($dst,$src,0,0,0,0,$width2,$height2,$size[0],$size[1]);
imagedestroy($src);
imagepng($dst,$fileName2); // adjust format as needed
imagedestroy($dst);
}
What I need is to save my image after resizing but in second code there is no condition check, and I can't get image upload folder path. That's why I need to merge these 2 codes.
Basically I need need to save my image in 3 size formats: 500x500,250x250 and 125x125. Width is fixed, but height is set as per aspect ratio and set upload folder and condition in second code block.
Try this thumbnail function, which takes your source image resource and thumbnail size, and returns a new image resource for the thumbnail.
function createThumbnail($src, int $width = 100, int $height = null) {
// Ensure that src is a valid gd resource
if (!(is_resource($src) && 'gd' === get_resource_type($src))) {
throw new InvalidArgumentException(
sprintf("Argument 1 of %s expected type resource(gd), %s supplied", __FUNCTION__, gettype($src))
);
}
// Extract source image width, height and aspect ratio
$source_width = imagesx($src);
$source_height = imagesy($src);
$ratio = $source_width / $source_height;
// We know that width is always supplied, and that height can be null
// We must solve height according to aspect ratio if null is supplied
if (null === $height) {
$height = round($width / $ratio);
}
// Create the thumbnail resampled resource
$thumb = imagecreatetruecolor($width, $height);
imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, $source_width, $source_height);
return $thumb;
}
Given your code above, you can now use the function like this
// Get uploaded file information as you have
// Create your source resource as you have
$source = imagecreatefromstring(file_get_contents($fn));
// Create thumbnails and save + destroy
$thumb = createThumbnail($source, 500);
imagejpeg($thumb, $thumb500TargetPath, 90);
imagedestroy($thumb);
$thumb = createThumbnail($source, 250);
imagejpeg($thumb, $thumb250TargetPath, 90);
imagedestroy($thumb);
$thumb = createThumbnail($source, 125);
imagejpeg($thumb, $thumb125TargetPath, 90);
imagedestroy($thumb);
// Don't forget to destroy the source resource
imagedestroy($source);

Scale Image Using PHP and Maintaining Aspect Ratio

Basically I want to upload an image (which i've sorted) and scale it down to certain constraints such as max width and height but maintain the aspect ratio of the original image.
I don't have Imagick installed on the server - otherwise this would be easy.
Any help is appreciated as always.
Thanks.
EDIT: I don't need the whole code or anything, just a push in the right direction would be fantastic.
Actually the accepted solution it is not the correct solution. The reason is simple: there will be cases when the ratio of the source image and the ratio of the destination image will be different. Any calculation should reflect this difference.
Please note the relevant lines from the example given on PHP.net website:
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
The full example may be found here:
http://php.net/manual/en/function.imagecopyresampled.php
There are other answers (with examples) on stackoverflow to similar questions (the same question formulated in a different manner) that suffer of the same problem.
Example:
Let's say we have an image of 1630 x 2400 pixels that we want to be auto resized keeping the aspect ratio to 160 x 240. Let's do some math taking the accepted solution:
if($old_x < $old_y)
{
$thumb_w = $old_x*($new_width/$old_y);
$thumb_h = $new_height;
}
height = 240
width = 1630 * ( 160/2400 ) = 1630 * 0.0666666666666667 = 108.6666666666667
108.6 x 240 it's not the correct solution.
The next solution proposed is the following:
if($old_x < $old_y)
{
$thumb_w = $old_x/$old_y*$newHeight;
$thumb_h = $newHeight;
}
height = 240;
width = 1630 / 2400 * 240 = 163
It is better (as it maintain the aspect ratio), but it exceeded the maximum accepted width.
Both fail.
We do the math according to the solution proposed by PHP.net:
width = 160
height = 160/(1630 / 2400) = 160/0.6791666666666667 = 235.5828220858896 (the else clause). 160 x 236 (rounded) is the correct answer.
I had written a peice of code like this for another project I've done. I've copied it below, might need a bit of tinkering! (It does required the GD library)
These are the parameters it needs:
$image_name - Name of the image which is uploaded
$new_width - Width of the resized photo (maximum)
$new_height - Height of the resized photo (maximum)
$uploadDir - Directory of the original image
$moveToDir - Directory to save the resized image
It will scale down or up an image to the maximum width or height
function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDir)
{
$path = $uploadDir . '/' . $image_name;
$mime = getimagesize($path);
if($mime['mime']=='image/png') {
$src_img = imagecreatefrompng($path);
}
if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
$src_img = imagecreatefromjpeg($path);
}
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
if($old_x > $old_y)
{
$thumb_w = $new_width;
$thumb_h = $old_y*($new_height/$old_x);
}
if($old_x < $old_y)
{
$thumb_w = $old_x*($new_width/$old_y);
$thumb_h = $new_height;
}
if($old_x == $old_y)
{
$thumb_w = $new_width;
$thumb_h = $new_height;
}
$dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// New save location
$new_thumb_loc = $moveToDir . $image_name;
if($mime['mime']=='image/png') {
$result = imagepng($dst_img,$new_thumb_loc,8);
}
if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
$result = imagejpeg($dst_img,$new_thumb_loc,80);
}
imagedestroy($dst_img);
imagedestroy($src_img);
return $result;
}
Formule is wrong for keeping aspect ratio.
It should be: original height / original width x new width = new height
function createThumbnail($imageName,$newWidth,$newHeight,$uploadDir,$moveToDir)
{
$path = $uploadDir . '/' . $imageName;
$mime = getimagesize($path);
if($mime['mime']=='image/png'){ $src_img = imagecreatefrompng($path); }
if($mime['mime']=='image/jpg'){ $src_img = imagecreatefromjpeg($path); }
if($mime['mime']=='image/jpeg'){ $src_img = imagecreatefromjpeg($path); }
if($mime['mime']=='image/pjpeg'){ $src_img = imagecreatefromjpeg($path); }
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
if($old_x > $old_y)
{
$thumb_w = $newWidth;
$thumb_h = $old_y/$old_x*$newWidth;
}
if($old_x < $old_y)
{
$thumb_w = $old_x/$old_y*$newHeight;
$thumb_h = $newHeight;
}
if($old_x == $old_y)
{
$thumb_w = $newWidth;
$thumb_h = $newHeight;
}
$dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// New save location
$new_thumb_loc = $moveToDir . $imageName;
if($mime['mime']=='image/png'){ $result = imagepng($dst_img,$new_thumb_loc,8); }
if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
imagedestroy($dst_img);
imagedestroy($src_img);
return $result;
}
I was thinking about how to achieve this and i came with a pretty nice solution that works in any case...
Lets say you want to resize heavy images that users upload to your site but you need it to maintain the ratio. So i came up with this :
<?php
// File
$filename = 'test.jpg';
// Get sizes
list($width, $height) = getimagesize($filename);
//obtain ratio
$imageratio = $width/$height;
if($imageratio >= 1){
$newwidth = 600;
$newheight = 600 / $imageratio;
}
else{
$newidth = 400;
$newheight = 400 / $imageratio;
};
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width,
$height);
// Output
imagejpeg($thumb, "img/test.jpg");
imagedestroy();
?>
In this case if width is bigger than height, i wanted the width to be 600px and if the height was bigger than the width, i wanted the width to be 400px
<?php
Class ResizedImage
{
public $imgfile;
public $string = '';
public $new_width = 0;
public $new_height = 0;
public $angle = 0;
public $max_font_size = 1000;
public $cropped = false;//whether crop the original image if h or w > new h or w
public $font = 'fonts/arialbd.ttf';
private $img;
private $trans_colour;
private $orange;
private $white;
private $whitetr;
private $blacktr;
public function PrintAsBase64()
{
$this->SetImage();
ob_start();
imagepng($this->img);
$b64img = ob_get_contents();
ob_clean();
imagedestroy($this->img);
$b64img = base64_encode($b64img);
echo($b64img);
}
public function PrintAsImage()
{
$this->SetImage();
header('Content-type: image/png');
imagepng($this->img);
imagedestroy($this->img);
}
private function SetImage()
{
if ($this->imgfile == '') {$this->imgfile='NoImageAvailable.jpg';}
$this->img = imagecreatefromstring(file_get_contents($this->imgfile));
$this->trans_colour = imagecolorallocatealpha($this->img, 0, 0, 0, 127);
$this->orange = imagecolorallocate($this->img, 220, 210, 60);
$this->white = imagecolorallocate($this->img, 255,255, 255);
$this->whitetr = imagecolorallocatealpha($this->img, 255,255, 255, 95);
$this->blacktr = imagecolorallocatealpha($this->img, 0, 0, 0, 95);
if ((!$this->cropped) && ($this->string !=''))
{$this->watermarkimage();}
if (($this->new_height > 0) && ($this->new_width > 0)) {$this->ResizeImage();};
if (($this->cropped) && ($this->string !=''))
{$this->watermarkimage();}
imageAlphaBlending($this->img, true);
imageSaveAlpha($this->img, true);
}
////
private function ResizeImage()
{
# v_fact and h_fact are the factor by which the original vertical / horizontal
# image sizes should be multiplied to get the image to your target size.
$v_fact = $this->new_height / imagesy($this->img);//target_height / im_height;
$h_fact = $this->new_width / imagesx($this->img);//target_width / im_width;
# you want to resize the image by the same factor in both vertical
# and horizontal direction, so you need to pick the correct factor from
# v_fact / h_fact so that the largest (relative to target) of the new height/width
# equals the target height/width and the smallest is lower than the target.
# this is the lowest of the two factors
if($this->cropped)
{ $im_fact = max($v_fact, $h_fact); }
else
{ $im_fact = min($v_fact, $h_fact); }
$new_height = round(imagesy($this->img) * $im_fact);
$new_width = round(imagesx($this->img) * $im_fact);
$img2 = $this->img;
$this->img = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($this->img, $img2, 0, 0, 0, 0, $new_width, $new_height, imagesx($img2), imagesy($img2));
$img2 = $this->img;
$this->img = imagecreatetruecolor($this->new_width, $this->new_height);
imagefill($this->img, 0, 0, $this->trans_colour);
$dstx = 0;
$dsty = 0;
if ($this->cropped)
{
if (imagesx($this->img) < imagesx($img2))
{ $dstx = round((imagesx($this->img)-imagesx($img2))/2); }
if (imagesy($this->img) < imagesy($img2))
{ $dsty = round((imagesy($this->img)-imagesy($img2))/2); }
}
else
{
if (imagesx($this->img) > imagesx($img2))
{ $dstx = round((imagesx($this->img)-imagesx($img2))/2); }
if (imagesy($this->img) > imagesy($img2))
{ $dsty = round((imagesy($this->img)-imagesy($img2))/2); }
}
imagecopy ( $this->img, $img2, $dstx, $dsty, 0, 0, imagesx($img2) , imagesy($img2));
imagedestroy($img2);
}
////
private function calculateTextBox($text,$fontFile,$fontSize,$fontAngle)
{
/************
simple function that calculates the *exact* bounding box (single pixel precision).
The function returns an associative array with these keys:
left, top: coordinates you will pass to imagettftext
width, height: dimension of the image you have to create
*************/
$rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
return array(
"left" => abs($minX) - 1,
"top" => abs($minY) - 1,
"width" => $maxX - $minX,
"height" => $maxY - $minY,
"box" => $rect );
}
private function watermarkimage($font_size=0)
{
if ($this->string == '')
{die('Watermark function call width empty string!');}
$box = $this->calculateTextBox($this->string, $this->font, $font_size, $this->angle);
while ( ($box['width'] < imagesx($this->img)) && ($box['height'] < imagesy($this->img)) && ($font_size <= $this->max_font_size) )
{
$font_size++;
$box = $this->calculateTextBox($this->string, $this->font, $font_size, $this->angle);
}
$font_size--;
$box = $this->calculateTextBox($this->string, $this->font, $font_size, $this->angle);
$vcenter = round((imagesy($this->img) / 2) + ($box['height'] / 2));
$hcenter = round((imagesx($this->img) - $box['width']) / 2 );
imagettftext($this->img, $font_size, $this->angle, $hcenter, $vcenter, $this->blacktr, $this->font, $this->string);
imagettftext($this->img, $font_size, $this->angle, $hcenter+1, $vcenter-2, $this->whitetr, $this->font, $this->string);
}
}
?>
Also I have been using the accepted answer but it does not keep the ratio in some cases. I have found some good answers on the forum and have put them in together and finally created a Class which resizes an image. As extra function u can put a watermark text.
u can see what happens when choose to crop or not, if not a transparent area will be added to the new resized image.
This example is more than asked, but I think it is a good example.
I know you are looking for a divisor that will allow resize your image proportionally. Check this demo
How to get our divisor mathematically
lets assume our original image has width x and height y;
x=300 and y = 700
Maximum height and maximum width is 200;
First, we will check which dimension of the image is greater than the other.
Our height (y) is greater than width(x)
Secondly, we check if our height is greater than our maximum height.
For our case, our height is greater than the maximum height. In a case where it less that the maximum height, we set our new height to our original height.
Finally, we look for our divisor as shown below
if y is set to maximum height 200 and max-y=200;
y=max-y, that is
if y=max-y
what about
x=?
that is,
if 700 is resized to 200
what about 300?
700=200
300=?
new width = (200 (new height) * 300(width)) / 700 (height)
so our divisor is
divisor= new height (300) / height(700)
new width = divisor * width or width / (1/divisor)
and vice versa for the width if it greater than height
if ($width > $height) {
if($width < $max_width)
$newwidth = $width;
else
$newwidth = $max_width;
$divisor = $width / $newwidth;
$newheight = floor( $height / $divisor);
}
else {
if($height < $max_height)
$newheight = $height;
else
$newheight = $max_height;
$divisor = $height / $newheight;
$newwidth = floor( $width / $divisor );
}
See the full example and try it using the working demo .
I found a mathematical way to get this job done
Github repo - https://github.com/gayanSandamal/easy-php-image-resizer
Live example - https://plugins.nayague.com/easy-php-image-resizer/
<?php
//path for the image
$source_url = '2018-04-01-1522613288.PNG';
//separate the file name and the extention
$source_url_parts = pathinfo($source_url);
$filename = $source_url_parts['filename'];
$extension = $source_url_parts['extension'];
//define the quality from 1 to 100
$quality = 10;
//detect the width and the height of original image
list($width, $height) = getimagesize($source_url);
$width;
$height;
//define any width that you want as the output. mine is 200px.
$after_width = 200;
//resize only when the original image is larger than expected with.
//this helps you to avoid from unwanted resizing.
if ($width > $after_width) {
//get the reduced width
$reduced_width = ($width - $after_width);
//now convert the reduced width to a percentage and round it to 2 decimal places
$reduced_radio = round(($reduced_width / $width) * 100, 2);
//ALL GOOD! let's reduce the same percentage from the height and round it to 2 decimal places
$reduced_height = round(($height / 100) * $reduced_radio, 2);
//reduce the calculated height from the original height
$after_height = $height - $reduced_height;
//Now detect the file extension
//if the file extension is 'jpg', 'jpeg', 'JPG' or 'JPEG'
if ($extension == 'jpg' || $extension == 'jpeg' || $extension == 'JPG' || $extension == 'JPEG') {
//then return the image as a jpeg image for the next step
$img = imagecreatefromjpeg($source_url);
} elseif ($extension == 'png' || $extension == 'PNG') {
//then return the image as a png image for the next step
$img = imagecreatefrompng($source_url);
} else {
//show an error message if the file extension is not available
echo 'image extension is not supporting';
}
//HERE YOU GO :)
//Let's do the resize thing
//imagescale([returned image], [width of the resized image], [height of the resized image], [quality of the resized image]);
$imgResized = imagescale($img, $after_width, $after_height, $quality);
//now save the resized image with a suffix called "-resized" and with its extension.
imagejpeg($imgResized, $filename . '-resized.'.$extension);
//Finally frees any memory associated with image
//**NOTE THAT THIS WONT DELETE THE IMAGE
imagedestroy($img);
imagedestroy($imgResized);
}
?>
This is my function to scale an image with save aspect ration for X, Y or both axes.
It's also scales an image for percent of it size.
Compatibe with PHP 5.4
/** Use X axis to scale image. */
define('IMAGES_SCALE_AXIS_X', 1);
/** Use Y axis to scale image. */
define('IMAGES_SCALE_AXIS_Y', 2);
/** Use both X and Y axes to calc image scale. */
define('IMAGES_SCALE_AXIS_BOTH', IMAGES_SCALE_AXIS_X ^ IMAGES_SCALE_AXIS_Y);
/** Compression rate for JPEG image format. */
define('JPEG_COMPRESSION_QUALITY', 90);
/** Compression rate for PNG image format. */
define('PNG_COMPRESSION_QUALITY', 9);
/**
* Scales an image with save aspect ration for X, Y or both axes.
*
* #param string $sourceFile Absolute path to source image.
* #param string $destinationFile Absolute path to scaled image.
* #param int|null $toWidth Maximum `width` of scaled image.
* #param int|null $toHeight Maximum `height` of scaled image.
* #param int|null $percent Percent of scale of the source image's size.
* #param int $scaleAxis Determines how of axis will be used to scale image.
*
* May take a value of {#link IMAGES_SCALE_AXIS_X}, {#link IMAGES_SCALE_AXIS_Y} or {#link IMAGES_SCALE_AXIS_BOTH}.
* #return bool True on success or False on failure.
*/
function scaleImage($sourceFile, $destinationFile, $toWidth = null, $toHeight = null, $percent = null, $scaleAxis = IMAGES_SCALE_AXIS_BOTH) {
$toWidth = (int)$toWidth;
$toHeight = (int)$toHeight;
$percent = (int)$percent;
$result = false;
if (($toWidth | $toHeight | $percent)
&& file_exists($sourceFile)
&& (file_exists(dirname($destinationFile)) || mkdir(dirname($destinationFile), 0777, true))) {
$mime = getimagesize($sourceFile);
if (in_array($mime['mime'], ['image/jpg', 'image/jpeg', 'image/pjpeg'])) {
$src_img = imagecreatefromjpeg($sourceFile);
} elseif ($mime['mime'] == 'image/png') {
$src_img = imagecreatefrompng($sourceFile);
}
$original_width = imagesx($src_img);
$original_height = imagesy($src_img);
if ($scaleAxis == IMAGES_SCALE_AXIS_BOTH) {
if (!($toWidth | $percent)) {
$scaleAxis = IMAGES_SCALE_AXIS_Y;
} elseif (!($toHeight | $percent)) {
$scaleAxis = IMAGES_SCALE_AXIS_X;
}
}
if ($scaleAxis == IMAGES_SCALE_AXIS_X && $toWidth) {
$scale_ratio = $original_width / $toWidth;
} elseif ($scaleAxis == IMAGES_SCALE_AXIS_Y && $toHeight) {
$scale_ratio = $original_height / $toHeight;
} elseif ($percent) {
$scale_ratio = 100 / $percent;
} else {
$scale_ratio_width = $original_width / $toWidth;
$scale_ratio_height = $original_height / $toHeight;
if ($original_width / $scale_ratio_width < $toWidth && $original_height / $scale_ratio_height < $toHeight) {
$scale_ratio = min($scale_ratio_width, $scale_ratio_height);
} else {
$scale_ratio = max($scale_ratio_width, $scale_ratio_height);
}
}
$scale_width = $original_width / $scale_ratio;
$scale_height = $original_height / $scale_ratio;
$dst_img = imagecreatetruecolor($scale_width, $scale_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $scale_width, $scale_height, $original_width, $original_height);
if (in_array($mime['mime'], ['image/jpg', 'image/jpeg', 'image/pjpeg'])) {
$result = imagejpeg($dst_img, $destinationFile, JPEG_COMPRESSION_QUALITY);
} elseif ($mime['mime'] == 'image/png') {
$result = imagepng($dst_img, $destinationFile, PNG_COMPRESSION_QUALITY);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
return $result;
}
Tests:
$sourceFile = '/source/file.jpg'; // Original size: 672x100
$destinationPath = '/destination/path/';
scaleImage($sourceFile, $destinationPath . 'file_original_size.jpg', 672, 100);
// Result: Image 672x100
scaleImage($sourceFile, $destinationPath . 'file_scaled_75_PERCENT.jpg', null, null, 75);
// Result: Image 504x75
scaleImage($sourceFile, $destinationPath . 'file_scaled_336_X.jpg', 336, null, null, IMAGES_SCALE_AXIS_X);
// Result: Image 336x50
scaleImage($sourceFile, $destinationPath . 'file_scaled_50_Y.jpg', null, 50, null, IMAGES_SCALE_AXIS_Y);
// Result: Image 336x50
scaleImage($sourceFile, $destinationPath . 'file_scaled_500x70_BOTH.jpg', 500, 70, null, IMAGES_SCALE_AXIS_BOTH);
// Result: Image 470x70
scaleImage($sourceFile, $destinationPath . 'file_scaled_450x70_BOTH.jpg', 450, 70, null, IMAGES_SCALE_AXIS_BOTH);
// Result: Image 450x66
scaleImage($sourceFile, $destinationPath . 'file_scaled_500x70_40_PERCENT_BOTH.jpg', 500, 70, 40, IMAGES_SCALE_AXIS_BOTH);
// Result: Image 268x40
Here is a comprehensive application that I worked hard on it to include most common operations like scale up & scale down, thumbnail, preserve aspect ratio, convert file type, change quality/file size and more...
<?php
//##// Resize (and convert) image (Scale up & scale down, thumbnail, preserve aspect ratio) //##//
///////////////////////////////////////////////
///////////////// Begin.Setup /////////////////
// Source File:
$src_file = "/your/server/path/to/file.png";// png or jpg files only
// Resize Dimensions:
// leave blank for no size change (convert only)
// if you specify one dimension, the other dimension will be calculated according to the aspect ratio
// if you specify both dimensions system will take care of it depending on the actual image size
// $newWidth = 2000;
// $newHeight = 1500;
// Destination Path: (optional, if none: download image)
$dst_path = "/your/server/path/new/";
// Destination File Name: (Leave blank for same file name)
// $dst_name = 'image_name_only_no_extension';
// Destination File Type: (Leave blank for same file extension)
// $dst_type = 'png';
$dst_type = 'jpg';
// Reduce to 8bit - 256 colors (Very low quality but very small file & transparent PNG. Only for thumbnails!)
// $palette_8bit = true;
///////////////// End.Setup /////////////////
///////////////////////////////////////////////
if (!$dst_name){$dst_name = strtolower(pathinfo($src_file, PATHINFO_FILENAME));}
if (!$dst_type){$dst_type = strtolower(pathinfo($src_file, PATHINFO_EXTENSION));}
if ($palette_8bit){$dst_type = 'png';}
if ($dst_path){$dst_file = $dst_path . $dst_name . '.' . $dst_type;}
$mime = getimagesize($src_file);// Get image dimensions and type
// Destination File Parameters:
if ($dst_type == 'png'){
$dst_content = 'image/png';
$quality = 9;// All same quality! 0 too big file // 0(no comp.)-9 (php default: 6)
} elseif ($dst_type == 'jpg'){
$dst_content = 'image/jpg';
$quality = 85;// 30 Min. 60 Mid. 85 Cool. 90 Max. (100 Full) // 0-100 (php default: 75)
} else {
exit('Unknown Destination File Type');
}
// Source File Parameters:
if ($mime['mime']=='image/png'){$src_img = imagecreatefrompng($src_file);}
elseif ($mime['mime']=='image/jpg'){$src_img = imagecreatefromjpeg($src_file);}
elseif ($mime['mime']=='image/jpeg'){$src_img = imagecreatefromjpeg($src_file);}
elseif ($mime['mime']=='image/pjpeg'){$src_img = imagecreatefromjpeg($src_file);}
else {exit('Unknown Source File Type');}
// Define Dimensions:
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
if ($newWidth AND $newHeight){
if($old_x > $old_y){
$new_x = $newWidth;
$new_y = $old_y / $old_x * $newWidth;
} elseif($old_x < $old_y){
$new_y = $newHeight;
$new_x = $old_x / $old_y * $newHeight;
} elseif($old_x == $old_y){
$new_x = $newWidth;
$new_y = $newHeight;
}
} elseif ($newWidth){
$new_x = $newWidth;
$new_y = $old_y / $old_x * $newWidth;
} elseif ($newHeight){
$new_y = $newHeight;
$new_x = $old_x / $old_y * $newHeight;
} else {
$new_x = $old_x;
$new_y = $old_y;
}
$dst_img = ImageCreateTrueColor($new_x, $new_y);
if ($palette_8bit){//////// Reduce to 8bit - 256 colors ////////
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagecolortransparent($dst_img, $transparent);
imagefill($dst_img, 0, 0, $transparent);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_x,$new_y,$old_x,$old_y);// Great quality resize.
imagetruecolortopalette($dst_img, false, 255);
imagesavealpha($dst_img, true);
} else {
// Check image and set transparent for png or white background for jpg
if ($dst_type == 'png'){
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagefilledrectangle($dst_img, 0, 0, $new_x, $new_y, $transparent);
} elseif ($dst_type == 'jpg'){
$white = imagecolorallocate($dst_img, 255, 255, 255);
imagefilledrectangle($dst_img, 0, 0, $new_x, $new_y, $white);
}
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_x,$new_y,$old_x,$old_y);// Great quality resize.
}
// Skip the save to parameter using NULL, then set the quality; imagejpeg($dst_img);=> Default quality
if ($dst_file){
if ($dst_type == 'png'){
imagepng($dst_img, $dst_file, $quality);
} elseif ($dst_type == 'jpg'){
imagejpeg($dst_img, $dst_file, $quality);
}
} else {
header('Content-Disposition: Attachment;filename=' . $dst_name . '.' . $dst_type);// comment this line to show image in browser instead of download
header('Content-type: ' . $dst_content);
if ($dst_type == 'png'){
imagepng($dst_img, NULL, $quality);
} elseif ($dst_type == 'jpg'){
imagejpeg($dst_img, NULL, $quality);
}
}
imagedestroy($src_img);
imagedestroy($dst_img);
//##// END : Resize image (Scale Up & Down) (thumbnail, bigger image, preserve aspect ratio) END //##//
works perfecly for me
static function getThumpnail($file){
$THUMBNAIL_IMAGE_MAX_WIDTH = 150; # exmpl.
$THUMBNAIL_IMAGE_MAX_HEIGHT = 150;
$src_size = filesize($file);
$filename = basename($file);
list($src_width, $src_height, $src_type) = getimagesize($file);
$src_im = false;
switch ($src_type) {
case IMAGETYPE_GIF : $src_im = imageCreateFromGif($file); break;
case IMAGETYPE_JPEG : $src_im = imageCreateFromJpeg($file); break;
case IMAGETYPE_PNG : $src_im = imageCreateFromPng($file); break;
case IMAGETYPE_WBMP : $src_im = imagecreatefromwbmp($file); break;
}
if ($src_im === false) { return false; }
$src_aspect_ratio = $src_width / $src_height;
$thu_aspect_ratio = $THUMBNAIL_IMAGE_MAX_WIDTH / $THUMBNAIL_IMAGE_MAX_HEIGHT;
if ($src_width <= $THUMBNAIL_IMAGE_MAX_WIDTH && $src_height <= $THUMBNAIL_IMAGE_MAX_HEIGHT) {
$thu_width = $src_width;
$thu_height = $src_height;
} elseif ($thu_aspect_ratio > $src_aspect_ratio) {
$thu_width = (int) ($THUMBNAIL_IMAGE_MAX_HEIGHT * $src_aspect_ratio);
$thu_height = $THUMBNAIL_IMAGE_MAX_HEIGHT;
} else {
$thu_width = $THUMBNAIL_IMAGE_MAX_WIDTH;
$thu_height = (int) ($THUMBNAIL_IMAGE_MAX_WIDTH / $src_aspect_ratio);
}
$thu_im = imagecreatetruecolor($thu_width, $thu_height);
imagecopyresampled($thu_im, $src_im, 0, 0, 0, 0, $thu_width, $thu_height, $src_width, $src_height);
$dst_im = imagecreatetruecolor($THUMBNAIL_IMAGE_MAX_WIDTH,$THUMBNAIL_IMAGE_MAX_WIDTH);
$backcolor = imagecolorallocate($dst_im,192,192,192);
imagefill($dst_im,0,0,$backcolor);
imagecopy($dst_im, $thu_im, (imagesx($dst_im)/2)-(imagesx($thu_im)/2), (imagesy($dst_im)/2)-(imagesy($thu_im)/2), 0, 0, imagesx($thu_im), imagesy($thu_im));
imagedestroy($src_im);
imagedestroy($thu_im);
}

Convert .psd and .ai to PNG/JPG with imagick

I'm creating thumbnails for a Digital asset manager, what is the best way to do this with imagemagick?
is there good resource out there?
I solved it and will share with the WORLD! it will convert .ai, .psd, .jpg, .png, .gif into thumbnails.
Here is a function that takes 4 params:
$dir - directory to save to.
$tmpName - the name to name the file excluding the extension.
$fileType - self explanatory.
$size - Large or small.
function thumbGenerator($dir,$tmpName,$fileType,$size){
$saveFileType = "png";
$imagePath = $dir.$tmpName.".".$fileType;
$image = new Imagick();
$image->readimage($imagePath);
if($fileType == "psd"){
$image->setIteratorIndex(0);
}
$dimensions = $image->getImageGeometry();
$width = $dimensions['width'];
$height = $dimensions['height'];
if($size == "large"){
$maxWidth = 720;
$maxHeight =720;
}
if($size == "small"){
$maxWidth = 250;
$maxHeight =250;
}
if($height > $width){
//Portrait
if($height > $maxHeight)
$image->thumbnailImage(0, $maxHeight);
$dimensions = $image->getImageGeometry();
if($dimensions['width'] > $maxWidth){
$image->thumbnailImage($maxWidth, 0);
}
}elseif($height < $width){
//Landscape
$image->thumbnailImage($maxWidth, 0);
}else{
//square
$image->thumbnailImage($maxWidth, 0);
}
if($size == "large"){
$image->writeImage($dir . $tmpName."-lg.".$saveFileType);
}
if($size == "small"){
$image->writeImage($dir . $tmpName."-sm.".$saveFileType);;
}
}
#Jason - Thanks for sharing. Here are a few tips for cleaner and easier to maintain/extend code. Again, a lot of it depends on your requirements. Also, I didn't actually run this code, so forgive any typos.
$dir - directory to save to.
$tmpName - the name to name the file excluding the extension.
$fileType - self explanatory.
$size - Large or small. You may consider taking a pixel width value for the thumbnail rather than a string for a predefined width. Let's say you will have the need for a larger thumbnail in a new section of your page in the future (i.e. Retina-ready icons with 500px for "small" thumbnails). You should preferably define the size in the new part of the code rather than in the shared thumbGenerator function
function thumbGenerator($dir,$tmpName,$fileType,$size){
$saveFileType = "png";
$imagePath = $dir.$tmpName.".".$fileType;
$image = new Imagick();
$image->readimage($imagePath);
if($fileType == "psd"){
$image->setIteratorIndex(0);
}
/* Simplify this code section below
$dimensions = $image->getImageGeometry();
$width = $dimensions['width'];
$height = $dimensions['height'];
*/
list($width,$height) = $image->getImageGeometry(); // <--- new code
/* Use $size for the pixel width/height instead and remove the code below
if($size == "large"){
$maxWidth = 720;
$maxHeight =720;
}
if($size == "small"){
$maxWidth = 250;
$maxHeight =250;
}
*/
if($height > $width){
//Portrait
if($height > $size)
$image->thumbnailImage(0, $size);
$dimensions = $image->getImageGeometry();
if($width > $size){ // <--- use the previously created $width variable
$image->thumbnailImage($size, 0);
}
/* Don't need this duplicate code.
}elseif($height < $width){
//Landscape
$image->thumbnailImage($maxWidth, 0);
*/
}else{
// square or landscape
$image->thumbnailImage($maxWidth, 0);
}
/* DRY - do not repeat yourself - Simplify it and use the pixel width in the image name
if($size == "large"){
$image->writeImage($dir . $tmpName."-lg.".$saveFileType);
}
if($size == "small"){
$image->writeImage($dir . $tmpName."-sm.".$saveFileType);;
}
*/
$image->writeImage($dir . $tmpName."-".$size.".".$saveFileType);;
}

php resize image after upload and crop it to the center

i have a user profile in php and i want to give users the choice of changing their profile picture. But when they submit their new picture via $_POST i want the picture to be resized to:
height:110px | width:relevant to the height (if the width is bigger than height)
width:110px | height:relevant to the width (if the height is bigger than width)
when the resize is done, i want to crop the picture so it becomes 110px x 110px but i want it to be centered.
For example if the user uploads a picture with 110px width and 200px height (dimensions after the resize) the new image after crop will be 110x110 cropped by 90px from right. What i want is to cropped 45px from left and another 45px from right so it will be centered
the function will accept .png, .gif and .jpg images and will save the new image only in jpg format no matter what the initial format was.
I searched a lot to create such a function and i found an answer but any time i atempt to change some minor detail everything stop working properly.
My code so far:
<?php
$userfile_name = $_FILES["sgnIMG"]["name"];
$userfile_tmp = $_FILES["sgnIMG"]["tmp_name"];
$userfile_size = $_FILES["sgnIMG"]["size"];
$filename = basename($_FILES["sgnIMG"]["name"]);
$file_ext = substr($filename, strrpos($filename, ".") + 1);
$large_image_location = $target_path . $filename;
$ext = '';
if ($file_ext == 'jpg') {
$ext = 1;
} else if ($file_ext == 'gif') {
$ext = 2;
} else if ($file_ext == 'png') {
$ext = 3;
} else {
$ext = 0;
}
$target = $target_path . basename($_FILES["sgnIMG"]["name"]);
if (move_uploaded_file($userfile_tmp, $target)) {
$newImg = resize110($target, $ext);
if (isset($_POST['imupd']) && ($_POST['imupd'] == 'up')) {
$sql = "UPDATE users SET avatar='" . str_replace('im/users/', '', $newImg) . "' WHERE id=" . $_SESSION['sesID'] . "";
$result = mysql_query($sql);
if ($result) {
echo '<img src="' . $newImg . '" width="110" title="' . $file_ext . '"/>';
} else {
echo '<img src="im/avatars/px.png" width="110" title="' . $file_ext . '"/>';
}
}
} else {
}
function getHeight($image)
{
$sizes = getimagesize($image);
$height = $sizes[1];
return $height;
}
function getWidth($image)
{
$sizes = getimagesize($image);
$width = $sizes[0];
return $width;
}
function resize110($image, $ext)
{
chmod($image, 0777);
$oldHeight = getHeight($image);
$oldWidth = getWidth($image);
if ($oldHeight < $oldWidth) {
$newImageHeight = 110;
$newImageWidth = ceil((110 * $oldWidth) / $oldHeight);
imagecopyresampled($newImage, $source, -ceil(($newImageWidth - 110) / 2), 0, 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight);
} else {
$newImageHeight = ceil((110 * $oldHeight) / $oldWidth);
$newImageWidth = 110;
imagecopyresampled($newImage, $source, 0, -ceil(($newImageHeight - 110) / 2), 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight);
}
$newImage = imagecreatetruecolor(110, 110);
chmod($image, 0777);
return $image;
switch ($ext) {
case 1;
$source = imagecreatefromjpeg($image);
break;
case 2;
$source = imagecreatefromgif($image);
break;
case 3;
$source = imagecreatefrompng($image);
break;
}
imagejpeg($newImage, $image, 90);
return $image;
}
I looked around a lot and combined different parts of code i found. So this script will take a jpg,gif of png image, resize it to 110px width if width is greater of 110px height if height is greater. The ascpect ratio will remain so the remaining pixels will be divided by 2 will be used to center the image.
for a different size just change 110 everywhere.
==================================================================================
<?php
// pfpic -> the name of the <input type="file" name="pfpic"/> where user chooses file
$target_path = "im/users/"; // the directory to store the uploaded and then resampled image
$userfile_name = $_FILES["pfpic"]["name"]; // the name that the image file will have once uploaded
$userfile_tmp = $_FILES["pfpic"]["tmp_name"]; // the temporary name the server uses to store the file
$userfile_size = $_FILES["pfpic"]["size"]; // the size of the file that we want to upload
$filename = basename($_FILES["pfpic"]["name"]); // the full name of the file
$file_ext = substr($filename, strrpos($filename, ".") + 1); // the file extension
$large_image_location = $target_path.$filename; // the full path to the file
$ext='';
if($file_ext=='jpg')
{
$ext=1;
}
else if ($file_ext=='gif')
{
$ext=2;
}
else if ($file_ext=='png')
{
$ext=3;
}
else
{
$ext=0;
}
$target = $target_path.basename(sha1($_SESSION['sesID']).'.'.'jpg');
if($ext!=0)
{
if(move_uploaded_file($userfile_tmp,$target))
{
$newImg=resize110($target,$ext);
echo '<img src="'.$newImg.'"/>';
}
else
{
echo 'the file could not be uploaded, please try again';
}
}
else
{
echo 'this file extension is not accepted, please use "jpg", "gif" or "png" file formats';
}
function getHeight($image)
{
$sizes = getimagesize($image);
$height = $sizes[1];
return $height;
}
function getWidth($image)
{
$sizes = getimagesize($image);
$width = $sizes[0];
return $width;
}
function resize110($image,$ext)
{
chmod($image, 0777);
$oldHeight=getHeight($image);
$oldWidth=getWidth($image);
switch ($ext)
{
case 1;
$source = imagecreatefromjpeg($image);
break;
case 2;
$source = imagecreatefromgif($image);
break;
case 3;
$source = imagecreatefrompng($image);
break;
}
$newImage = imagecreatetruecolor(110,110);
$bgcolor = imagecolorallocate($newImage, 255, 255, 255);
imagefill($newImage, 0, 0, $bgcolor); // use this if you want to have a white background instead of black
// we check tha width and height and then we crop the image to the center
if($oldHeight<$oldWidth)
{
$newImageHeight = 110;
$newImageWidth = ceil((110*$oldWidth)/$oldHeight);
imagecopyresampled($newImage,$source,-ceil(($newImageWidth-110)/2),0,0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight);
}
else
{
$newImageHeight = ceil((110*$oldHeight)/$oldWidth);
$newImageWidth = 110;
imagecopyresampled($newImage,$source,0,-ceil(($newImageHeight-110)/2),0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight);
}
//we save the image as jpg resized to 110x110 px and cropped to the center. the old image will be replaced
imagejpeg($newImage,$image,90);
return $image;
}
?>

Image GD resize to 100px while keep the ratio [duplicate]

Basically I want to upload an image (which i've sorted) and scale it down to certain constraints such as max width and height but maintain the aspect ratio of the original image.
I don't have Imagick installed on the server - otherwise this would be easy.
Any help is appreciated as always.
Thanks.
EDIT: I don't need the whole code or anything, just a push in the right direction would be fantastic.
Actually the accepted solution it is not the correct solution. The reason is simple: there will be cases when the ratio of the source image and the ratio of the destination image will be different. Any calculation should reflect this difference.
Please note the relevant lines from the example given on PHP.net website:
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
The full example may be found here:
http://php.net/manual/en/function.imagecopyresampled.php
There are other answers (with examples) on stackoverflow to similar questions (the same question formulated in a different manner) that suffer of the same problem.
Example:
Let's say we have an image of 1630 x 2400 pixels that we want to be auto resized keeping the aspect ratio to 160 x 240. Let's do some math taking the accepted solution:
if($old_x < $old_y)
{
$thumb_w = $old_x*($new_width/$old_y);
$thumb_h = $new_height;
}
height = 240
width = 1630 * ( 160/2400 ) = 1630 * 0.0666666666666667 = 108.6666666666667
108.6 x 240 it's not the correct solution.
The next solution proposed is the following:
if($old_x < $old_y)
{
$thumb_w = $old_x/$old_y*$newHeight;
$thumb_h = $newHeight;
}
height = 240;
width = 1630 / 2400 * 240 = 163
It is better (as it maintain the aspect ratio), but it exceeded the maximum accepted width.
Both fail.
We do the math according to the solution proposed by PHP.net:
width = 160
height = 160/(1630 / 2400) = 160/0.6791666666666667 = 235.5828220858896 (the else clause). 160 x 236 (rounded) is the correct answer.
I had written a peice of code like this for another project I've done. I've copied it below, might need a bit of tinkering! (It does required the GD library)
These are the parameters it needs:
$image_name - Name of the image which is uploaded
$new_width - Width of the resized photo (maximum)
$new_height - Height of the resized photo (maximum)
$uploadDir - Directory of the original image
$moveToDir - Directory to save the resized image
It will scale down or up an image to the maximum width or height
function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDir)
{
$path = $uploadDir . '/' . $image_name;
$mime = getimagesize($path);
if($mime['mime']=='image/png') {
$src_img = imagecreatefrompng($path);
}
if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
$src_img = imagecreatefromjpeg($path);
}
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
if($old_x > $old_y)
{
$thumb_w = $new_width;
$thumb_h = $old_y*($new_height/$old_x);
}
if($old_x < $old_y)
{
$thumb_w = $old_x*($new_width/$old_y);
$thumb_h = $new_height;
}
if($old_x == $old_y)
{
$thumb_w = $new_width;
$thumb_h = $new_height;
}
$dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// New save location
$new_thumb_loc = $moveToDir . $image_name;
if($mime['mime']=='image/png') {
$result = imagepng($dst_img,$new_thumb_loc,8);
}
if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
$result = imagejpeg($dst_img,$new_thumb_loc,80);
}
imagedestroy($dst_img);
imagedestroy($src_img);
return $result;
}
Formule is wrong for keeping aspect ratio.
It should be: original height / original width x new width = new height
function createThumbnail($imageName,$newWidth,$newHeight,$uploadDir,$moveToDir)
{
$path = $uploadDir . '/' . $imageName;
$mime = getimagesize($path);
if($mime['mime']=='image/png'){ $src_img = imagecreatefrompng($path); }
if($mime['mime']=='image/jpg'){ $src_img = imagecreatefromjpeg($path); }
if($mime['mime']=='image/jpeg'){ $src_img = imagecreatefromjpeg($path); }
if($mime['mime']=='image/pjpeg'){ $src_img = imagecreatefromjpeg($path); }
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
if($old_x > $old_y)
{
$thumb_w = $newWidth;
$thumb_h = $old_y/$old_x*$newWidth;
}
if($old_x < $old_y)
{
$thumb_w = $old_x/$old_y*$newHeight;
$thumb_h = $newHeight;
}
if($old_x == $old_y)
{
$thumb_w = $newWidth;
$thumb_h = $newHeight;
}
$dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// New save location
$new_thumb_loc = $moveToDir . $imageName;
if($mime['mime']=='image/png'){ $result = imagepng($dst_img,$new_thumb_loc,8); }
if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
imagedestroy($dst_img);
imagedestroy($src_img);
return $result;
}
I was thinking about how to achieve this and i came with a pretty nice solution that works in any case...
Lets say you want to resize heavy images that users upload to your site but you need it to maintain the ratio. So i came up with this :
<?php
// File
$filename = 'test.jpg';
// Get sizes
list($width, $height) = getimagesize($filename);
//obtain ratio
$imageratio = $width/$height;
if($imageratio >= 1){
$newwidth = 600;
$newheight = 600 / $imageratio;
}
else{
$newidth = 400;
$newheight = 400 / $imageratio;
};
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width,
$height);
// Output
imagejpeg($thumb, "img/test.jpg");
imagedestroy();
?>
In this case if width is bigger than height, i wanted the width to be 600px and if the height was bigger than the width, i wanted the width to be 400px
<?php
Class ResizedImage
{
public $imgfile;
public $string = '';
public $new_width = 0;
public $new_height = 0;
public $angle = 0;
public $max_font_size = 1000;
public $cropped = false;//whether crop the original image if h or w > new h or w
public $font = 'fonts/arialbd.ttf';
private $img;
private $trans_colour;
private $orange;
private $white;
private $whitetr;
private $blacktr;
public function PrintAsBase64()
{
$this->SetImage();
ob_start();
imagepng($this->img);
$b64img = ob_get_contents();
ob_clean();
imagedestroy($this->img);
$b64img = base64_encode($b64img);
echo($b64img);
}
public function PrintAsImage()
{
$this->SetImage();
header('Content-type: image/png');
imagepng($this->img);
imagedestroy($this->img);
}
private function SetImage()
{
if ($this->imgfile == '') {$this->imgfile='NoImageAvailable.jpg';}
$this->img = imagecreatefromstring(file_get_contents($this->imgfile));
$this->trans_colour = imagecolorallocatealpha($this->img, 0, 0, 0, 127);
$this->orange = imagecolorallocate($this->img, 220, 210, 60);
$this->white = imagecolorallocate($this->img, 255,255, 255);
$this->whitetr = imagecolorallocatealpha($this->img, 255,255, 255, 95);
$this->blacktr = imagecolorallocatealpha($this->img, 0, 0, 0, 95);
if ((!$this->cropped) && ($this->string !=''))
{$this->watermarkimage();}
if (($this->new_height > 0) && ($this->new_width > 0)) {$this->ResizeImage();};
if (($this->cropped) && ($this->string !=''))
{$this->watermarkimage();}
imageAlphaBlending($this->img, true);
imageSaveAlpha($this->img, true);
}
////
private function ResizeImage()
{
# v_fact and h_fact are the factor by which the original vertical / horizontal
# image sizes should be multiplied to get the image to your target size.
$v_fact = $this->new_height / imagesy($this->img);//target_height / im_height;
$h_fact = $this->new_width / imagesx($this->img);//target_width / im_width;
# you want to resize the image by the same factor in both vertical
# and horizontal direction, so you need to pick the correct factor from
# v_fact / h_fact so that the largest (relative to target) of the new height/width
# equals the target height/width and the smallest is lower than the target.
# this is the lowest of the two factors
if($this->cropped)
{ $im_fact = max($v_fact, $h_fact); }
else
{ $im_fact = min($v_fact, $h_fact); }
$new_height = round(imagesy($this->img) * $im_fact);
$new_width = round(imagesx($this->img) * $im_fact);
$img2 = $this->img;
$this->img = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($this->img, $img2, 0, 0, 0, 0, $new_width, $new_height, imagesx($img2), imagesy($img2));
$img2 = $this->img;
$this->img = imagecreatetruecolor($this->new_width, $this->new_height);
imagefill($this->img, 0, 0, $this->trans_colour);
$dstx = 0;
$dsty = 0;
if ($this->cropped)
{
if (imagesx($this->img) < imagesx($img2))
{ $dstx = round((imagesx($this->img)-imagesx($img2))/2); }
if (imagesy($this->img) < imagesy($img2))
{ $dsty = round((imagesy($this->img)-imagesy($img2))/2); }
}
else
{
if (imagesx($this->img) > imagesx($img2))
{ $dstx = round((imagesx($this->img)-imagesx($img2))/2); }
if (imagesy($this->img) > imagesy($img2))
{ $dsty = round((imagesy($this->img)-imagesy($img2))/2); }
}
imagecopy ( $this->img, $img2, $dstx, $dsty, 0, 0, imagesx($img2) , imagesy($img2));
imagedestroy($img2);
}
////
private function calculateTextBox($text,$fontFile,$fontSize,$fontAngle)
{
/************
simple function that calculates the *exact* bounding box (single pixel precision).
The function returns an associative array with these keys:
left, top: coordinates you will pass to imagettftext
width, height: dimension of the image you have to create
*************/
$rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
return array(
"left" => abs($minX) - 1,
"top" => abs($minY) - 1,
"width" => $maxX - $minX,
"height" => $maxY - $minY,
"box" => $rect );
}
private function watermarkimage($font_size=0)
{
if ($this->string == '')
{die('Watermark function call width empty string!');}
$box = $this->calculateTextBox($this->string, $this->font, $font_size, $this->angle);
while ( ($box['width'] < imagesx($this->img)) && ($box['height'] < imagesy($this->img)) && ($font_size <= $this->max_font_size) )
{
$font_size++;
$box = $this->calculateTextBox($this->string, $this->font, $font_size, $this->angle);
}
$font_size--;
$box = $this->calculateTextBox($this->string, $this->font, $font_size, $this->angle);
$vcenter = round((imagesy($this->img) / 2) + ($box['height'] / 2));
$hcenter = round((imagesx($this->img) - $box['width']) / 2 );
imagettftext($this->img, $font_size, $this->angle, $hcenter, $vcenter, $this->blacktr, $this->font, $this->string);
imagettftext($this->img, $font_size, $this->angle, $hcenter+1, $vcenter-2, $this->whitetr, $this->font, $this->string);
}
}
?>
Also I have been using the accepted answer but it does not keep the ratio in some cases. I have found some good answers on the forum and have put them in together and finally created a Class which resizes an image. As extra function u can put a watermark text.
u can see what happens when choose to crop or not, if not a transparent area will be added to the new resized image.
This example is more than asked, but I think it is a good example.
I know you are looking for a divisor that will allow resize your image proportionally. Check this demo
How to get our divisor mathematically
lets assume our original image has width x and height y;
x=300 and y = 700
Maximum height and maximum width is 200;
First, we will check which dimension of the image is greater than the other.
Our height (y) is greater than width(x)
Secondly, we check if our height is greater than our maximum height.
For our case, our height is greater than the maximum height. In a case where it less that the maximum height, we set our new height to our original height.
Finally, we look for our divisor as shown below
if y is set to maximum height 200 and max-y=200;
y=max-y, that is
if y=max-y
what about
x=?
that is,
if 700 is resized to 200
what about 300?
700=200
300=?
new width = (200 (new height) * 300(width)) / 700 (height)
so our divisor is
divisor= new height (300) / height(700)
new width = divisor * width or width / (1/divisor)
and vice versa for the width if it greater than height
if ($width > $height) {
if($width < $max_width)
$newwidth = $width;
else
$newwidth = $max_width;
$divisor = $width / $newwidth;
$newheight = floor( $height / $divisor);
}
else {
if($height < $max_height)
$newheight = $height;
else
$newheight = $max_height;
$divisor = $height / $newheight;
$newwidth = floor( $width / $divisor );
}
See the full example and try it using the working demo .
I found a mathematical way to get this job done
Github repo - https://github.com/gayanSandamal/easy-php-image-resizer
Live example - https://plugins.nayague.com/easy-php-image-resizer/
<?php
//path for the image
$source_url = '2018-04-01-1522613288.PNG';
//separate the file name and the extention
$source_url_parts = pathinfo($source_url);
$filename = $source_url_parts['filename'];
$extension = $source_url_parts['extension'];
//define the quality from 1 to 100
$quality = 10;
//detect the width and the height of original image
list($width, $height) = getimagesize($source_url);
$width;
$height;
//define any width that you want as the output. mine is 200px.
$after_width = 200;
//resize only when the original image is larger than expected with.
//this helps you to avoid from unwanted resizing.
if ($width > $after_width) {
//get the reduced width
$reduced_width = ($width - $after_width);
//now convert the reduced width to a percentage and round it to 2 decimal places
$reduced_radio = round(($reduced_width / $width) * 100, 2);
//ALL GOOD! let's reduce the same percentage from the height and round it to 2 decimal places
$reduced_height = round(($height / 100) * $reduced_radio, 2);
//reduce the calculated height from the original height
$after_height = $height - $reduced_height;
//Now detect the file extension
//if the file extension is 'jpg', 'jpeg', 'JPG' or 'JPEG'
if ($extension == 'jpg' || $extension == 'jpeg' || $extension == 'JPG' || $extension == 'JPEG') {
//then return the image as a jpeg image for the next step
$img = imagecreatefromjpeg($source_url);
} elseif ($extension == 'png' || $extension == 'PNG') {
//then return the image as a png image for the next step
$img = imagecreatefrompng($source_url);
} else {
//show an error message if the file extension is not available
echo 'image extension is not supporting';
}
//HERE YOU GO :)
//Let's do the resize thing
//imagescale([returned image], [width of the resized image], [height of the resized image], [quality of the resized image]);
$imgResized = imagescale($img, $after_width, $after_height, $quality);
//now save the resized image with a suffix called "-resized" and with its extension.
imagejpeg($imgResized, $filename . '-resized.'.$extension);
//Finally frees any memory associated with image
//**NOTE THAT THIS WONT DELETE THE IMAGE
imagedestroy($img);
imagedestroy($imgResized);
}
?>
This is my function to scale an image with save aspect ration for X, Y or both axes.
It's also scales an image for percent of it size.
Compatibe with PHP 5.4
/** Use X axis to scale image. */
define('IMAGES_SCALE_AXIS_X', 1);
/** Use Y axis to scale image. */
define('IMAGES_SCALE_AXIS_Y', 2);
/** Use both X and Y axes to calc image scale. */
define('IMAGES_SCALE_AXIS_BOTH', IMAGES_SCALE_AXIS_X ^ IMAGES_SCALE_AXIS_Y);
/** Compression rate for JPEG image format. */
define('JPEG_COMPRESSION_QUALITY', 90);
/** Compression rate for PNG image format. */
define('PNG_COMPRESSION_QUALITY', 9);
/**
* Scales an image with save aspect ration for X, Y or both axes.
*
* #param string $sourceFile Absolute path to source image.
* #param string $destinationFile Absolute path to scaled image.
* #param int|null $toWidth Maximum `width` of scaled image.
* #param int|null $toHeight Maximum `height` of scaled image.
* #param int|null $percent Percent of scale of the source image's size.
* #param int $scaleAxis Determines how of axis will be used to scale image.
*
* May take a value of {#link IMAGES_SCALE_AXIS_X}, {#link IMAGES_SCALE_AXIS_Y} or {#link IMAGES_SCALE_AXIS_BOTH}.
* #return bool True on success or False on failure.
*/
function scaleImage($sourceFile, $destinationFile, $toWidth = null, $toHeight = null, $percent = null, $scaleAxis = IMAGES_SCALE_AXIS_BOTH) {
$toWidth = (int)$toWidth;
$toHeight = (int)$toHeight;
$percent = (int)$percent;
$result = false;
if (($toWidth | $toHeight | $percent)
&& file_exists($sourceFile)
&& (file_exists(dirname($destinationFile)) || mkdir(dirname($destinationFile), 0777, true))) {
$mime = getimagesize($sourceFile);
if (in_array($mime['mime'], ['image/jpg', 'image/jpeg', 'image/pjpeg'])) {
$src_img = imagecreatefromjpeg($sourceFile);
} elseif ($mime['mime'] == 'image/png') {
$src_img = imagecreatefrompng($sourceFile);
}
$original_width = imagesx($src_img);
$original_height = imagesy($src_img);
if ($scaleAxis == IMAGES_SCALE_AXIS_BOTH) {
if (!($toWidth | $percent)) {
$scaleAxis = IMAGES_SCALE_AXIS_Y;
} elseif (!($toHeight | $percent)) {
$scaleAxis = IMAGES_SCALE_AXIS_X;
}
}
if ($scaleAxis == IMAGES_SCALE_AXIS_X && $toWidth) {
$scale_ratio = $original_width / $toWidth;
} elseif ($scaleAxis == IMAGES_SCALE_AXIS_Y && $toHeight) {
$scale_ratio = $original_height / $toHeight;
} elseif ($percent) {
$scale_ratio = 100 / $percent;
} else {
$scale_ratio_width = $original_width / $toWidth;
$scale_ratio_height = $original_height / $toHeight;
if ($original_width / $scale_ratio_width < $toWidth && $original_height / $scale_ratio_height < $toHeight) {
$scale_ratio = min($scale_ratio_width, $scale_ratio_height);
} else {
$scale_ratio = max($scale_ratio_width, $scale_ratio_height);
}
}
$scale_width = $original_width / $scale_ratio;
$scale_height = $original_height / $scale_ratio;
$dst_img = imagecreatetruecolor($scale_width, $scale_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $scale_width, $scale_height, $original_width, $original_height);
if (in_array($mime['mime'], ['image/jpg', 'image/jpeg', 'image/pjpeg'])) {
$result = imagejpeg($dst_img, $destinationFile, JPEG_COMPRESSION_QUALITY);
} elseif ($mime['mime'] == 'image/png') {
$result = imagepng($dst_img, $destinationFile, PNG_COMPRESSION_QUALITY);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
return $result;
}
Tests:
$sourceFile = '/source/file.jpg'; // Original size: 672x100
$destinationPath = '/destination/path/';
scaleImage($sourceFile, $destinationPath . 'file_original_size.jpg', 672, 100);
// Result: Image 672x100
scaleImage($sourceFile, $destinationPath . 'file_scaled_75_PERCENT.jpg', null, null, 75);
// Result: Image 504x75
scaleImage($sourceFile, $destinationPath . 'file_scaled_336_X.jpg', 336, null, null, IMAGES_SCALE_AXIS_X);
// Result: Image 336x50
scaleImage($sourceFile, $destinationPath . 'file_scaled_50_Y.jpg', null, 50, null, IMAGES_SCALE_AXIS_Y);
// Result: Image 336x50
scaleImage($sourceFile, $destinationPath . 'file_scaled_500x70_BOTH.jpg', 500, 70, null, IMAGES_SCALE_AXIS_BOTH);
// Result: Image 470x70
scaleImage($sourceFile, $destinationPath . 'file_scaled_450x70_BOTH.jpg', 450, 70, null, IMAGES_SCALE_AXIS_BOTH);
// Result: Image 450x66
scaleImage($sourceFile, $destinationPath . 'file_scaled_500x70_40_PERCENT_BOTH.jpg', 500, 70, 40, IMAGES_SCALE_AXIS_BOTH);
// Result: Image 268x40
Here is a comprehensive application that I worked hard on it to include most common operations like scale up & scale down, thumbnail, preserve aspect ratio, convert file type, change quality/file size and more...
<?php
//##// Resize (and convert) image (Scale up & scale down, thumbnail, preserve aspect ratio) //##//
///////////////////////////////////////////////
///////////////// Begin.Setup /////////////////
// Source File:
$src_file = "/your/server/path/to/file.png";// png or jpg files only
// Resize Dimensions:
// leave blank for no size change (convert only)
// if you specify one dimension, the other dimension will be calculated according to the aspect ratio
// if you specify both dimensions system will take care of it depending on the actual image size
// $newWidth = 2000;
// $newHeight = 1500;
// Destination Path: (optional, if none: download image)
$dst_path = "/your/server/path/new/";
// Destination File Name: (Leave blank for same file name)
// $dst_name = 'image_name_only_no_extension';
// Destination File Type: (Leave blank for same file extension)
// $dst_type = 'png';
$dst_type = 'jpg';
// Reduce to 8bit - 256 colors (Very low quality but very small file & transparent PNG. Only for thumbnails!)
// $palette_8bit = true;
///////////////// End.Setup /////////////////
///////////////////////////////////////////////
if (!$dst_name){$dst_name = strtolower(pathinfo($src_file, PATHINFO_FILENAME));}
if (!$dst_type){$dst_type = strtolower(pathinfo($src_file, PATHINFO_EXTENSION));}
if ($palette_8bit){$dst_type = 'png';}
if ($dst_path){$dst_file = $dst_path . $dst_name . '.' . $dst_type;}
$mime = getimagesize($src_file);// Get image dimensions and type
// Destination File Parameters:
if ($dst_type == 'png'){
$dst_content = 'image/png';
$quality = 9;// All same quality! 0 too big file // 0(no comp.)-9 (php default: 6)
} elseif ($dst_type == 'jpg'){
$dst_content = 'image/jpg';
$quality = 85;// 30 Min. 60 Mid. 85 Cool. 90 Max. (100 Full) // 0-100 (php default: 75)
} else {
exit('Unknown Destination File Type');
}
// Source File Parameters:
if ($mime['mime']=='image/png'){$src_img = imagecreatefrompng($src_file);}
elseif ($mime['mime']=='image/jpg'){$src_img = imagecreatefromjpeg($src_file);}
elseif ($mime['mime']=='image/jpeg'){$src_img = imagecreatefromjpeg($src_file);}
elseif ($mime['mime']=='image/pjpeg'){$src_img = imagecreatefromjpeg($src_file);}
else {exit('Unknown Source File Type');}
// Define Dimensions:
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
if ($newWidth AND $newHeight){
if($old_x > $old_y){
$new_x = $newWidth;
$new_y = $old_y / $old_x * $newWidth;
} elseif($old_x < $old_y){
$new_y = $newHeight;
$new_x = $old_x / $old_y * $newHeight;
} elseif($old_x == $old_y){
$new_x = $newWidth;
$new_y = $newHeight;
}
} elseif ($newWidth){
$new_x = $newWidth;
$new_y = $old_y / $old_x * $newWidth;
} elseif ($newHeight){
$new_y = $newHeight;
$new_x = $old_x / $old_y * $newHeight;
} else {
$new_x = $old_x;
$new_y = $old_y;
}
$dst_img = ImageCreateTrueColor($new_x, $new_y);
if ($palette_8bit){//////// Reduce to 8bit - 256 colors ////////
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagecolortransparent($dst_img, $transparent);
imagefill($dst_img, 0, 0, $transparent);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_x,$new_y,$old_x,$old_y);// Great quality resize.
imagetruecolortopalette($dst_img, false, 255);
imagesavealpha($dst_img, true);
} else {
// Check image and set transparent for png or white background for jpg
if ($dst_type == 'png'){
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagefilledrectangle($dst_img, 0, 0, $new_x, $new_y, $transparent);
} elseif ($dst_type == 'jpg'){
$white = imagecolorallocate($dst_img, 255, 255, 255);
imagefilledrectangle($dst_img, 0, 0, $new_x, $new_y, $white);
}
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_x,$new_y,$old_x,$old_y);// Great quality resize.
}
// Skip the save to parameter using NULL, then set the quality; imagejpeg($dst_img);=> Default quality
if ($dst_file){
if ($dst_type == 'png'){
imagepng($dst_img, $dst_file, $quality);
} elseif ($dst_type == 'jpg'){
imagejpeg($dst_img, $dst_file, $quality);
}
} else {
header('Content-Disposition: Attachment;filename=' . $dst_name . '.' . $dst_type);// comment this line to show image in browser instead of download
header('Content-type: ' . $dst_content);
if ($dst_type == 'png'){
imagepng($dst_img, NULL, $quality);
} elseif ($dst_type == 'jpg'){
imagejpeg($dst_img, NULL, $quality);
}
}
imagedestroy($src_img);
imagedestroy($dst_img);
//##// END : Resize image (Scale Up & Down) (thumbnail, bigger image, preserve aspect ratio) END //##//
works perfecly for me
static function getThumpnail($file){
$THUMBNAIL_IMAGE_MAX_WIDTH = 150; # exmpl.
$THUMBNAIL_IMAGE_MAX_HEIGHT = 150;
$src_size = filesize($file);
$filename = basename($file);
list($src_width, $src_height, $src_type) = getimagesize($file);
$src_im = false;
switch ($src_type) {
case IMAGETYPE_GIF : $src_im = imageCreateFromGif($file); break;
case IMAGETYPE_JPEG : $src_im = imageCreateFromJpeg($file); break;
case IMAGETYPE_PNG : $src_im = imageCreateFromPng($file); break;
case IMAGETYPE_WBMP : $src_im = imagecreatefromwbmp($file); break;
}
if ($src_im === false) { return false; }
$src_aspect_ratio = $src_width / $src_height;
$thu_aspect_ratio = $THUMBNAIL_IMAGE_MAX_WIDTH / $THUMBNAIL_IMAGE_MAX_HEIGHT;
if ($src_width <= $THUMBNAIL_IMAGE_MAX_WIDTH && $src_height <= $THUMBNAIL_IMAGE_MAX_HEIGHT) {
$thu_width = $src_width;
$thu_height = $src_height;
} elseif ($thu_aspect_ratio > $src_aspect_ratio) {
$thu_width = (int) ($THUMBNAIL_IMAGE_MAX_HEIGHT * $src_aspect_ratio);
$thu_height = $THUMBNAIL_IMAGE_MAX_HEIGHT;
} else {
$thu_width = $THUMBNAIL_IMAGE_MAX_WIDTH;
$thu_height = (int) ($THUMBNAIL_IMAGE_MAX_WIDTH / $src_aspect_ratio);
}
$thu_im = imagecreatetruecolor($thu_width, $thu_height);
imagecopyresampled($thu_im, $src_im, 0, 0, 0, 0, $thu_width, $thu_height, $src_width, $src_height);
$dst_im = imagecreatetruecolor($THUMBNAIL_IMAGE_MAX_WIDTH,$THUMBNAIL_IMAGE_MAX_WIDTH);
$backcolor = imagecolorallocate($dst_im,192,192,192);
imagefill($dst_im,0,0,$backcolor);
imagecopy($dst_im, $thu_im, (imagesx($dst_im)/2)-(imagesx($thu_im)/2), (imagesy($dst_im)/2)-(imagesy($thu_im)/2), 0, 0, imagesx($thu_im), imagesy($thu_im));
imagedestroy($src_im);
imagedestroy($thu_im);
}

Categories