Resizing image with exact width and height in php - php

I am sure this topic has been discussed number of times and I have started doing this from yesterday evening but till now no complete satisfaction.I am using the following code and it gives me a resized image but the scaling is not correct.The height and width is not in it's correct proportion.
I couldn't find any good fiddle like jsfiddle where you can see the output so pasting my code here.
You can see the original image url here also I have attached a resized image.
http://distilleryimage4.s3.amazonaws.com/b1da08e4484511e38e4d0a7011810191_7.jpg
$filename = 'http://distilleryimage4.s3.amazonaws.com/b1da08e4484511e38e4d0a7011810191_7.jpg';
//the resize will be a percent of the original size
$percent = 0.589;
$percent_height = 0.294;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent_height;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb);
imagedestroy($thumb);

This is basically a Math problem:
You are scaling width and height to both different proportions ($percent and $percent_height). You should scale both of them (width and height) by the same percent, to get an image resized to the same ratio. Maybe I didn't understand your question but I think you should change this line:
$newheight = $height * $percent_height;
to
$newheight = $height * $percent;
(and remove $percent_height = 0.294; if we are not gonna use it here)

I would use Imagick. I was doing resizing with imagecreatetruecolor, but it takes a lot of time (0,5 seconds) to resize 1920*1080 image to 150*120.
If you still want to use imagecreatetruecolor: to get the correct scalling use this code.

Related

How do I Resize images to fixed width & height while maintaing aspect ratio in PHP?

I am trying to batch resize images to the size of 250 x 250 in PHP
All source images are way bigger than 250 x 250 so that is helpful.
I want to maintain aspect ratio but make them all 250 x 250. I know that a portion of the image will be cropped off to do this. That is not an issue for me
The problem is that my current script only works on width and makes height according to aspect but sometimes, the image will now end up being let's say, 250 x 166. I can't use that.
In that cause It would need to be resized in the opposite manner (height to width)
How would the script have to look to always make the final image 250 x 250 without stretching. Again, I don't care if there is cropping. I assume there is going to be an else in the somewhere but this is way over my head now. I am more of a front end guy.
Any help would be great.
Below is just the relevant portion of my full script:
$width = 250;
$height = true;
// download and create gd image
$image = ImageCreateFromString(file_get_contents($url));
// calculate resized ratio
// Note: if $height is set to TRUE then we automatically calculate the height based on the ratio
$height = $height === true ? (ImageSY($image) * $width / ImageSX($image)) : $height;
// create image
$output = ImageCreateTrueColor($width, $height);
ImageCopyResampled($output, $image, 0, 0, 0, 0, $width, $height, ImageSX($image), ImageSY($image));
// save image
ImageJPEG($output, $destdir, 100);
$newWidth = 250;
$newHeight = 250;
// download and create gd image
$image = ImageCreateFromString(file_get_contents($url));
$width = ImageSX($image);
$height = ImageSY($image);
$coefficient = $newHeight / $height;
if ($newHeight / $width > $coefficient) {
$coefficient = $newHeight / $width;
}
// create image
$output = ImageCreateTrueColor($newWidth, $newHeight);
ImageCopyResampled($output, $image, 0, 0, 0, 0, $width * $coefficient, $height * $coefficient, $width, $height);
// save image
ImageJPEG($output, $destdir, 100);

PHP imagecopyresampled fills whole destination image, but i need to fill only part

Source image width is 344 pixels and height is 86 pixels.
I create new blank image (destination) width 64 pixels and height also 64 pixels.
Then i want to resize source image, so that width is 64 pixels and height is proportionally less.
I did:
Get source image size.
$size = getimagesize( $_FILES['file_to_upload']['tmp_name'] );
Then set new width and height. Initial width is 344 pixels, i need 64, so new width is initial width divided with proportion (344 / 64). New height also is initial height divided with proportion.
$size[0] = $size[0]/($size[0]/64);
$size[1] = $size[1]/($size[0]/64);
Expecting that initial image resizes so that width will be 64 pixels and height 16 pixels.
Create initial image
$src = imagecreatefromstring(file_get_contents( $_FILES['file_to_upload']['tmp_name'] ));
Create destination image
$dst = imagecreatetruecolor($width_64,$height_64);
Create necessary image
imagecopyresampled($dst,$src,0,0,0,0,$width_64,$height_64,$size[0],$size[1]);
imagepng($dst, $img_directory. '/pngicon64_64.png' );
But as result i get this
But i nee to get this
As understand code does not resample initial image. Just take part of it.
May be instead of imagecopyresampled need to use something else?
You have to calculate the image ratio, check which dimension is greater & reduce that one to your desired size, then calculate the smaller dimension using the image ratio.
An extremely similar example is available in the PHP documenation for the imagecopyresampled function. I adapted that example to the code you provided:
// The file
$filename = $_FILES['file_to_upload']['tmp_name'];
// Set a maximum height and width
$width = 64;
$height = 64;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatetruecolor($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagepng($image_p, $img_directory. '/pngicon64_64.png');

PHP - a little more than how to combine two images while keeping transparency

So I already know how to combine two images and keep transparency using the GD image library with php however I need to be able to pull images from the server while keeping the dimensions in the power of two so that I can use them as textures to be applied to 3d objects in opengl.
So my real question is how can I place a scaled version of an image that keeps its original proportions within a transparent one that has a 256x256 or 128x128 dimension. Also Id like to place the resized image within the center of the completely transparent one.
Some help on this would be awesome.
Check out this references, I think it's what you want: http://php.net/manual/en/function.imagecopyresized.php
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>

Proportional image resizing

i want to resize uploaded images to width: 180px with proportional height. Is there any classes to do this?
Thanks for help!
I think this question can use an answer with an actual code example. The code below shows you how you to resize an image inside a directory uploaded, and save the resized image in the folder resized.
<?php
// the file
$filename = 'uploaded/my_image.jpg';
// the desired width of the image
$width = 180;
// content type
header('Content-Type: image/jpeg');
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
$height = $width/$ratio_orig;
// resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// output
imagejpeg($image_p, 'resized/my_image.jpg', 80);
?>
First you need to get the current image dimensions:
$width = imagesx($image);
$height = imagesy($image);
Then calculate the scaling factor:
$scalingFactor = $newImageWidth / $width;
When having the scaling factor just calculate the new height of the image:
$newImageHeight = $height * $scalingFactor;
Then just create the new image;
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);
Probably these snippets will help:
http://www.codeslices.net/snippets/resize-scale-image-proportionally-to-given-width-in-php http://www.codeslices.net/snippets/resize-scale-image-proportionally-in-php
at least they worked for me.
you may use imagecopyresampled php function. new sizes you also can calculate.
User jquery plugin JCrop, and set its aspect ratio for the image...
Check this link for details:
http://www.webresourcesdepot.com/jquery-image-crop-plugin-jcrop/

php gd: when i crop images through php some images come out smushed

here is the website im talking about
http://makeupbyarpi.com/portfolio.php
you'll notice some of the images are smushed width-wise.
the code i used is this:
$width="500";
$height="636";
$img_src = $_FILES['galleryimg']['tmp_name'];
$thumb = "../gallery/".rand(0,100000).".jpg";
//Create image stream
$image = imagecreatefromjpeg($img_src);
//Gather and store the width and height
list($image_width, $image_height) = getimagesize($img_src);
//Resample/resize the image
$tmp_img = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
//Attempt to save the new thumbnail
if(is_writeable(dirname($thumb))){
imagejpeg($tmp_img, $thumb, 100);
}
//Free memory
imagedestroy($tmp_img);
imagedestroy($image);
the images that get uploaded are huge sometimes 3000px by 2000px and i have php crop it down to 500 x 536 and some landscape based images get smushed. is there a formula i can use to crop it carefully so that the image comes out good?
thanks
You could resize and add a letterbox if required. You simply need to resize the width and then calculate the new height (assuming width to height ratio is same as original) then if the height is not equal to the preferred height you need to draw a black rectangle (cover background) and then centre the image.
You could also do a pillarbox, but then you do the exact same as above except that width becomes height and height becomes width.
Edit: Actually, you resize the one that is the biggest, if width is bigger, you resize that and if height is bigger then you resize that. And depending on which one you resize, your script should either letterbox or pillarbox.
EDIT 2:
<?php
// Define image to resize
$img_src = $_FILES['galleryimg']['tmp_name'];
$thumb = "../gallery/" . rand(0,100000) . ".jpg";
// Define resize width and height
$width = 500;
$height = 636;
// Open image
$img = imagecreatefromjpeg($img_src);
// Store image width and height
list($img_width, $img_height) = getimagesize($img_src);
// Create the new image
$new_img = imagecreatetruecolor($width, $height);
// Calculate stuff and resize image accordingly
if (($width/$img_width) < ($height/$img_height)) {
$new_width = $width;
$new_height = ($width/$img_width) * $img_height;
$new_x = 0;
$new_y = ($height - $new_height) / 2;
} else {
$new_width = ($height/$img_height) * $img_width;
$new_height = $height;
$new_x = ($width - $new_width) / 2;
$new_y = 0;
}
imagecopyresampled($new_img, $img, $new_x, $new_y, 0, 0, $new_width, $new_height, $img_width, $img_height);
// Save thumbnail
if (is_writeable(dirname($thumb))) {
imagejpeg($new_img, $thumb, 100);
}
// Free up resources
imagedestroy($new_img);
imagedestroy($img);
?>
Sorry it took a while, I ran across a small bug in the calculation part which I was unable to fix for like 10 minutes =/ This should work.

Categories