Proportional image resizing - php

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/

Related

How to up-scale an image {PHP}

I used the code below to get an image:
$url = "http://maps.googleapis.com/maps/api/staticmap?size=800x600&zoom=12&center=(10.763705518561297,106.64444580078126)";
$content = file_get_contents($url);
I received a 640px x 640px image meanwhile I expected a 800px x 600px one. How do I scale that image to 800px x 600px?
Try doing this:
<?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);
?>
Here, rescaling is done by 50%, you can change parameter as per your requirement. You can even use Imagick for image manipulation. See, if that helps.
Referred: PHP Manual

How can I force resize the image without following the original scale?

I am going to grab some images from different websites and resize and the download them. I am using the following script:
$img = file_get_contents($picarr[0]);
$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
if($width > $height){
$newwidth = 600;
$newheight = (600*$height)/$width;
}else{
$newheight = 400;
$newwidth = (400*$width)/$height;
}
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'D:\mypic\9.jpg', 100); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
this code is worked, but it seems it's following the original photo's scale (which is 4:3, and the size of original picture is 720 x 540). So the output is 600 x 450. How can I or is it possible to force resize the image to 600 x 400 ?
EDIT: Also, I am trying to add some watermark on the image.I know the way to add watermark when I submit the form, but how can I do this while running this script

Downloading an image with php and at the same time resizing it

I want to download this image:
http://imgs.xkcd.com/clickdrag/1n2w.png
But the image is too large for me so i want to resize it to lets say 100 times smaller than it is now. Also i want the image to have its original name (in this case 1n2w.png).
For downloading i was thinking of using somet
$content = file_get_contents('http://imgs.xkcd.com/clickdrag/1n2w.png');
file_put_contents('/images', $content);
But it didnt work. Maybe i need to use curl for this?
As for the resizing part i dont know what to use, so if possible i would like too see some suggestions on this.
For image re-sizing you can use it.
$percent = 0.5;
$filename = '/home/Pictures/downloaded_file.jpg';
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename)
;
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, 100);

php image resize creating image with lower quality

I am using the simple image resize methode
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, 100);
Eventhoug i had given the quality to maximum , when it comes to smaller image size , the generated image is of very poor quality (original 500px to new 100px ).
Is there any other way to increase the image quality ?
Yes there is a way to increase the quality: use ImageMagick instead of the GD library, if possible. The quality of the GD library is pretty poor.
try this
<?php
header('Content-type: image/jpeg');
$image = new Imagick('image.jpg');
// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$image->thumbnailImage(100, 0);
echo $image;
?>
source http://php.net/manual/en/imagick.examples-1.php

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