PHP crop image to fix width and height without losing dimension ratio - php

im looking to create thumbnails that has 100px by 100px dimension. i've seen many articles explaining the methods but most end up having the width!=height if the dimension ratio is to be kept.
for example, i have a 450px by 350px image. i would like to crop to 100px by 100px. if i were to keep the ratio, i would end up having 100px by 77px. this makes it ugly when im listing these images in a row and column. however, a image without dimension ratio will look terrible as well.
i've seen images from flickr and they look fantastic. for example:
thumbnail: http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
medium size: http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
large size: http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg
tks

This is done by only using a part of the image as the thumbnail which has a 1:1 aspect ratio (mostly the center of the image). If you look closely you can see it in the flickr thumbnail.
Because you have "crop" in your question, I'm not sure if you didn't already know this, but what do you want to know then?
To use cropping, here is an example:
//Your Image
$imgSrc = "image.jpg";
//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);
//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);

Crop image with square based on lesser width or height
public function croppThis($target_url) {
$this->jpegImgCrop($target_url);
}
$target_url - is Name of image.
public function jpegImgCrop($target_url) {//support
$image = imagecreatefromjpeg($target_url);
$filename = $target_url;
$width = imagesx($image);
$height = imagesy($image);
$image_type = imagetypes($image); //IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM
if($width==$height) {
$thumb_width = $width;
$thumb_height = $height;
} elseif($width<$height) {
$thumb_width = $width;
$thumb_height = $width;
} elseif($width>$height) {
$thumb_width = $height;
$thumb_height = $height;
} else {
$thumb_width = 150;
$thumb_height = 150;
}
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect ) {
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else {
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}

You can use this code.
You need to pass source image path and thumbnail size in px, and optional destination path. if you pass it will save the image else the thumb will be shown.
Only jpg, jpeg and png are allowed.
function cropImage($sourcePath, $thumbSize, $destination = null) {
$parts = explode('.', $sourcePath);
$ext = $parts[count($parts) - 1];
if ($ext == 'jpg' || $ext == 'jpeg') {
$format = 'jpg';
} else {
$format = 'png';
}
if ($format == 'jpg') {
$sourceImage = imagecreatefromjpeg($sourcePath);
}
if ($format == 'png') {
$sourceImage = imagecreatefrompng($sourcePath);
}
list($srcWidth, $srcHeight) = getimagesize($sourcePath);
// calculating the part of the image to use for thumbnail
if ($srcWidth > $srcHeight) {
$y = 0;
$x = ($srcWidth - $srcHeight) / 2;
$smallestSide = $srcHeight;
} else {
$x = 0;
$y = ($srcHeight - $srcWidth) / 2;
$smallestSide = $srcWidth;
}
$destinationImage = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($destinationImage, $sourceImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
if ($destination == null) {
header('Content-Type: image/jpeg');
if ($format == 'jpg') {
imagejpeg($destinationImage, null, 100);
}
if ($format == 'png') {
imagejpeg($destinationImage);
}
if ($destination = null) {
}
} else {
if ($format == 'jpg') {
imagejpeg($destinationImage, $destination, 100);
}
if ($format == 'png') {
imagepng($destinationImage, $destination);
}
}
}

I like using GDLib to fiddle with images, it's fantastically easy to work with also. There are lots of blog posts and tutorials out there. I would recommend using a class for it or similar though, as taking care of all the variations in the image can be very time consuming.

To complete #SvenKoschnicke code, here is little tool to process other image formats:
$sourceProperties = getimagesize($imgSrc);
$width = $sourceProperties[0];
$height = $sourceProperties[1];
switch ($sourceProperties[2]) {
case IMAGETYPE_PNG:
$myImage = imagecreatefrompng($imgSrc);
break;
case IMAGETYPE_GIF:
$myImage = imagecreatefromgif($imgSrc);
break;
case IMAGETYPE_JPEG:
$myImage = imagecreatefromjpeg($imgSrc);
break;
}

Related

resize / crop image and add white space

I have this resize and crop function :
$file = imagecreatefromjpeg($imagefile["tmp_name"]);
if($imagefile["type"] == "image/jpg" || $imagefile["type"] == "image/jpeg") {
$uploaded_file = "/var/www/adimages/".$fid."-".$imagefile["name"];
} elseif($imagefile["type"] == "image/png") {
$uploaded_file = "/var/www/adimages/".$fid."-".$imagefile["name"];
}
imagejpeg($file, $uploaded_file, 100);
chmod($uploaded_file, 0644);
list($width, $height, $type, $attr) = getimagesize($uploaded_file);
$this->imageSizeW = $width;
$this->imageSizeH = $height;
$this->newSizeW = 250;
$this->newSizeH = 400;
$aspect = $width / $height;
$new_aspect = $this->newSizeW / $this->newSizeH;
if($aspect >= $new_aspect) {
$new_height = $this->newSizeH;
$new_width = $width / ($height / $this->newSizeH);
} else {
$new_width = $this->newSizeW;
$new_height = $height / ($width / $this->newSizeW);
}
$img_source = imagecreatefromjpeg($uploaded_file);
$newimage = imagecreatetruecolor($this->newSizeW, $this->newSizeH);
imagecopyresampled($newimage, $img_source, 0 - ($new_width - $this->newSizeW) / 2, 0 - ($new_height - $this->newSizeH), 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($newimage, $uploaded_file, 100);
}
That works as intended, almost - when it crops it removes part of the image.
Can I somehow extend / configure this function so that it will resize / center the image and add whitespace so the image will be as original but still 250 x 400 px
Using Class upload 0.32 from voret.net instead - this does what I want!

Saving a jpg as a transparent png with php

I have a function created which processes images in different ways based on the parameters. I have comments in the function to explain a bit.
Everything works with exception to when I take a jpg image and save it as a png. I just checked, even if I use imagepng() but save it with the extension .jpg, the image displays while properly resized, but with a .jpg extension. However, if I upload the same .jpg image, use imagepng() and save it with a .png extension, I get an image of the expected width and height after the resize, in a png format, with a .png extension. While the whole image is 100% transparent I have a 1px by 1px black pixel in the top left corner.
I would appreciate it if anyone can look at this and see if they see something I am missing. I believe the 1px X 1px is coming from the point I use for the imagefill(), but I don't understand why; it should be filling all transparent as with the rest of the image.
Here is my function:
if(!function_exists("upload")){
//$image = $image file name
//$width = intended width of resized image, if 0 it will proportion to height, overrides proportion
//$height = intended width of resized image, if 0 it will proportion to width, overrides proportion
//$proportion = 2,1,0;
//------ 2 = Preserve proportions while adding a border to fill width and height
//------ 1 = retain proportion to fit within both given width and height
//------ 0 = disregard proportions and resize to the exact width and height
function upload($image, $width, $height, $proportion){
// IS GD HERE?
$gdv = get_gd_info();
if (!$gdv){
return FALSE;
}
// GET AN IMAGE THING
$ext = trim(strtolower(end(explode('.', $image))));
list($imageX, $imageY, $type) = getimagesize($image); //gets information about new server image
// GET THE LESSER OF THE RATIO OF THUMBNAIL H OR W DIMENSIONS
$ratio_w = ($width / $imageX);
$ratio_h = ($height / $imageY);
$ratio = ($ratio_w < $ratio_h) ? $ratio_w : $ratio_h;
if($width == 0){
if($imageY > $height){
$newHeight = $height;
$newWidth = ($height/$imageY) * $imageX;
}else{
$newHeight = $imageY;
$newWidth = $imageX;
}
$width = $newWidth;
$height = $newHeight;
// COMPUTE THUMBNAIL IMAGE CENTERING OFFSETS
$fromMidX = 0;
$fromMidY = 0;
}elseif($height == 0){
if ($imageX > $width){
$newWidth = $width;
$newHeight = ($width/$imageX) * $imageY;
}else{
$newHeight = $imageY;
$newWidth = $imageX;
}
$width = $newWidth;
$height = $newHeight;
// COMPUTE THUMBNAIL IMAGE CENTERING OFFSETS
$fromMidX = 0;
$fromMidY = 0;
}elseif($proportion == 2){
// COMPUTE THUMBNAIL IMAGE DIMENSIONS
$newWidth = $imageX * $ratio;
$newHeight = $imageY * $ratio;
// COMPUTE THUMBNAIL IMAGE CENTERING OFFSETS
$fromMidX = ($width - $newWidth) / 2.0;
$fromMidY = ($height - $newHeight) / 2.0;
}elseif($proportion == 1){
if ($imageX > $width){
$newHeight = ($width/$imageX) * $imageY;
$newWidth = $width;
}
if ($imageY > $height){
$newHeight = $height;
$newWidth = ($height/$imageY) * $imageX;
}
$fromMidY = 0;
$fromMidX = 0;
}elseif($proportion == 0){
$newWidth = $width;
$newHeight = $height;
$fromMidY = 0;
$fromMidX = 0;
}
switch(strtoupper($ext))
{
case 'JPG' :
case 'JPEG' :
$source = imagecreatefromjpeg($image);
break;
case 'PNG' :
$source = imagecreatefrompng($image);
break;
default : die("UNKNOWN IMAGE TYPE: $image");
}
// WHICH FUNCTIONS CAN RESIZE / RESAMPLE THE IMAGE?
if ($gdv >= 2)
{
// IF GD IS AT LEVEL 2 OR ABOVE
$target = imagecreatetruecolor($width, $height);
$color = imagecolorallocatealpha($target, 0, 0, 0, 127);
imagefill($target, 0, 0, $color);
imagesavealpha($target, TRUE);
imagecopyresampled ($target, $source, $fromMidX, $fromMidY, 0, 0, $newWidth, $newHeight, $imageX, $imageY);
}
else
{
// IF GD IS AT A LOWER REVISION LEVEL
$target = imagecreate($width, $height);
imagesavealpha($target, TRUE);
$empty = imagecolorallocatealpha($thumb,0x00,0x00,0x00,127);
imagefill($target, 0, 0, $empty);
imagecopyresized($target, $source, $fromMidX, $fromMidY, 0, 0, $newWidth, $newHeight, $imageX, $imageY);
}
// SHARPEN THE PIC
$sharpenMatrix = array
( array( -1.2, -1, -1.2 )
, array( -1, 20, -1 )
, array( -1.2, -1, -1.2 )
)
;
$divisor = array_sum(array_map('array_sum', $sharpenMatrix));
$offset = 0;
imageconvolution($target, $sharpenMatrix, $divisor, $offset);
if(imagepng($target, $image,9)){
imagedestroy($target);
}else{
}
return $image;
}
}
EDIT 1: I guess I should have noted that I am uploading a .jpg image (Ex: 100px X 200px) and converting them to .png (Ex: 400px X 200px) but am retaining the proportions of the image to fit perfectly in the center of the destination .png. So I would have a 400px X 200px .png with my image in the center and 100px on the left and right which should be transparent. This way it fits nicely in a slider without a solid color as a fill. So an example of my call to my function would be upload($image, 400, 200, 2).
I figured out what the issue was. The portion of code which decides whether I should use imagecreatefromjpeg or imagecreatefrompng was incorrectly going off of the extension of the passed image and NOT by its mime type. I switched it to this:
switch($type)
{
case '2' :
$source = imagecreatefromjpeg($image);
break;
case '3' :
$source = imagecreatefrompng($image);
break;
default : die("UNKNOWN IMAGE TYPE: $image");
}
where $type is coming from the line I had in there
list($imageX, $imageY, $type) = getimagesize($image);
So all together the function is:
if(!function_exists("upload")){
//$image = $image file name
//$width = intended width of resized image, if 0 it will proportion to height, overrides proportion
//$height = intended width of resized image, if 0 it will proportion to width, overrides proportion
//$proportion = 2,1,0;
//------ 2 = Preserve proportions while adding a border to fill width and height
//------ 1 = retain proportion to fit within both given width and height
//------ 0 = disregard proportions and resize to the exact width and height
function upload($image, $width, $height, $proportion){
// IS GD HERE?
$gdv = get_gd_info();
if (!$gdv){
return FALSE;
}
// GET AN IMAGE THING
$ext = trim(strtolower(end(explode('.', $image))));
list($imageX, $imageY, $type) = getimagesize($image); //gets information about new server image
// GET THE LESSER OF THE RATIO OF THUMBNAIL H OR W DIMENSIONS
$ratio_w = ($width / $imageX);
$ratio_h = ($height / $imageY);
$ratio = ($ratio_w < $ratio_h) ? $ratio_w : $ratio_h;
if($width == 0){
if($imageY > $height){
$newHeight = $height;
$newWidth = ($height/$imageY) * $imageX;
}else{
$newHeight = $imageY;
$newWidth = $imageX;
}
$width = $newWidth;
$height = $newHeight;
// COMPUTE THUMBNAIL IMAGE CENTERING OFFSETS
$fromMidX = 0;
$fromMidY = 0;
}elseif($height == 0){
if ($imageX > $width){
$newWidth = $width;
$newHeight = ($width/$imageX) * $imageY;
}else{
$newHeight = $imageY;
$newWidth = $imageX;
}
$width = $newWidth;
$height = $newHeight;
// COMPUTE THUMBNAIL IMAGE CENTERING OFFSETS
$fromMidX = 0;
$fromMidY = 0;
}elseif($proportion == 2){
// COMPUTE THUMBNAIL IMAGE DIMENSIONS
$newWidth = $imageX * $ratio;
$newHeight = $imageY * $ratio;
// COMPUTE THUMBNAIL IMAGE CENTERING OFFSETS
$fromMidX = ($width - $newWidth) / 2.0;
$fromMidY = ($height - $newHeight) / 2.0;
}elseif($proportion == 1){
if ($imageX > $width){
$newHeight = ($width/$imageX) * $imageY;
$newWidth = $width;
}
if ($imageY > $height){
$newHeight = $height;
$newWidth = ($height/$imageY) * $imageX;
}
$fromMidY = 0;
$fromMidX = 0;
}elseif($proportion == 0){
$newWidth = $width;
$newHeight = $height;
$fromMidY = 0;
$fromMidX = 0;
}
switch($type)
{
case '2' :
$source = imagecreatefromjpeg($image);
break;
case '3' :
$source = imagecreatefrompng($image);
break;
default : die("UNKNOWN IMAGE TYPE: $image");
}
// WHICH FUNCTIONS CAN RESIZE / RESAMPLE THE IMAGE?
if ($gdv >= 2)
{
// IF GD IS AT LEVEL 2 OR ABOVE
$target = imagecreatetruecolor($width, $height);
$color = imagecolorallocatealpha($target, 0, 0, 0, 127);
imagefill($target, 0, 0, $color);
imagesavealpha($target, TRUE);
imagecopyresampled ($target, $source, $fromMidX, $fromMidY, 0, 0, $newWidth, $newHeight, $imageX, $imageY);
}
else
{
// IF GD IS AT A LOWER REVISION LEVEL
$target = imagecreate($width, $height);
imagesavealpha($target, TRUE);
$empty = imagecolorallocatealpha($thumb,0x00,0x00,0x00,127);
imagefill($target, 0, 0, $empty);
imagecopyresized($target, $source, $fromMidX, $fromMidY, 0, 0, $newWidth, $newHeight, $imageX, $imageY);
}
// SHARPEN THE PIC
$sharpenMatrix = array
( array( -1.2, -1, -1.2 )
, array( -1, 20, -1 )
, array( -1.2, -1, -1.2 )
)
;
$divisor = array_sum(array_map('array_sum', $sharpenMatrix));
$offset = 0;
imageconvolution($target, $sharpenMatrix, $divisor, $offset);
if(imagepng($target, $image,9)){
imagedestroy($target);
}
return $image;
}
}
And an example call would be: upload("path/to/image/on/server.png", 400, 200, 2)
Where the image path can be either in a jpg or png mime type. But the extension MUST be .png. I suppose I can make it smarter where you can have any extension, but with the current code which uses this function it makes more sense for me to leave it as is. I would like to eventually add the option to turn on or off the transparency and instead use a solid color such as black if it is needed. Feel free to use and modify this.
There are nice hex2rgb/rgb2hex functions which I plan on using for this here

PHP imagecopyresampled() without height

I have this piece of code here
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
Baiscally what I am trying to do is upload an image and resize the width and have the height adjusted based on the width.
I tried this also
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width);
without the height and got this error
Warning: Wrong parameter count for imagecopyresampled() in /home/content/44/8713044/html/admin/Categories.php on line 63
this is the current code where the $width and $height variables come from.
if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
Any Help would be appreciated, Thanks in advanced,
J
Here is the full function..
function create_thumbnail($source,$destination, $thumb_width) {
$percent = 0.5;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
$x = 0;
$y = 0;
if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
$new_image = imagecreatetruecolor($thumb_width,$thumb_width)or die('Cannot Initialize new GD image stream');
$extension = get_image_extension($source);
if($extension=='jpg' || $extension=='jpeg')
$image = imagecreatefromjpeg($source);
if($extension=='gif')
$image = imagecreatefromgif($source);
if($extension=='png')
$image = imagecreatefrompng($source);
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
if($extension=='jpg' || $extension=='jpeg')
imagejpeg($new_image,$destination);
if($extension=='gif')
imagegif($new_image,$destination);
if($extension=='png')
imagepng($new_image,$destination);
}
the $thumb_width is 600 and this returns my image 600*600
This worked perfectly when i tested it .... Except you want to work with fixed size
$filename = "a.jpg" ;
$percent = 0.5;
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$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);
Demo

Changing image width when sending via header in php

I have this piece of code that calls a random image and sends it to another page. That page calls this script via an image link. What I am trying to figure out is how to make it specify an image width when the image is past my max width. I got this far then got completely lost.
<?php
$folder = '';
$exts = 'jpg jpeg png gif';
$files = array(); $i = -1;
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) {
if (preg_match('/\.'.$ext.'$/i', $file, $test)) {
$files[] = $file;
++$i;
}
}
}
closedir($handle);
mt_srand((double)microtime()*1000000);
$rand = mt_rand(0, $i);
$image =($folder.$files[$rand]);
if (file_exists($image))
{
list($width) = getimagesize($image);
$maxWidth = 150;
if ($width > $maxWidth)
{
header('Location: '.$folder.$files[$rand]); // Voila!;
}
else
{
header('Location: '.$folder.$files[$rand]); // Voila!
}
}
?>
You could use CSS max-width and max-height properties.
Here isa couple of resize functions, I think at least one of them maintains the aspect ratio.
function resize_image($file, $w, $h, $crop = false) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*($r-$w/$h)));
} else {
$height = ceil($height-($height*($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
And:
function thumb_aspect_crop($source_path, $desired_width, $desired_height) {
list( $source_width, $source_height, $source_type ) = getimagesize($source_path);
switch ($source_type) {
case IMAGETYPE_GIF:
$source_gdim = imagecreatefromgif($source_path);
break;
case IMAGETYPE_JPEG:
$source_gdim = imagecreatefromjpeg($source_path);
break;
case IMAGETYPE_PNG:
$source_gdim = imagecreatefrompng($source_path);
break;
}
$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = $desired_width / $desired_height;
if ($source_aspect_ratio > $desired_aspect_ratio) {
// Triggered when source image is wider
$temp_height = $desired_height;
$temp_width = (int) ( $desired_height * $source_aspect_ratio );
} else {
// Triggered otherwise (i.e. source image is similar or taller)
$temp_width = $desired_width;
$temp_height = (int) ( $desired_width / $source_aspect_ratio );
}
// Resize the image into a temporary GD image
$temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
imagecopyresampled(
$temp_gdim,
$source_gdim,
0, 0,
0, 0,
$temp_width, $temp_height,
$source_width, $source_height
);
// Copy cropped region from temporary image into the desired GD image
$x0 = ( $temp_width - $desired_width ) / 2;
$y0 = ( $temp_height - $desired_height ) / 2;
$desired_gdim = imagecreatetruecolor($desired_width, $desired_height);
imagecopy(
$desired_gdim,
$temp_gdim,
0, 0,
$x0, $y0,
$desired_width, $desired_height
);
return $desired_gdim;
}
Try to use Primage class, like in example
You can not specify that in the header. There are 2 ways to go about it.
1 : in the image tag, specify the width attribute, however this will force the smaller images to scale up as well, so probably not the best course of action.
2 : Resize the image on the fly with GD library and send the resultant image instead of the original.
Edit:
For resizing you may look into this very simple to use image resize class.
http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToWidth($maxWidth);
$image->save('picture2.jpg');
Now you can redirect to picture2.jpg or send the image content inline. Also if the image is used often, then check on top of the script, if your resized image exists, if it does send it otherwise continue to resize.

Image resize PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can anybody suggest the best image resize script in php?
I'm still a newbie regarding image handling or file handling for that matter in PHP.
Would appreciate any input regarding the following
I post an image file using a simple html form and upload it via php.
When i try and alter my code to accomodate larger files (i.e. resize) I get an error.
Have been searching online but cant find anything really simple.
$size = getimagesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size == FALSE)
{
$errors=1;
}else if($size[0] > 300){ //if width greater than 300px
$aspectRatio = 300 / $size[0];
$newWidth = round($aspectRatio * $size[0]);
$newHeight = round($aspectRatio * $size[1]);
$imgHolder = imagecreatetruecolor($newWidth,$newHeight);
}
$newname= ROOTPATH.LOCALDIR."/images/".$image_name; //image_name is generated
$copy = imagecopyresized($imgHolder, $_FILES['image']['tmp_name'], 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]);
move_uploaded_file($copy, $newname); //where I want to move the file to the location of $newname
The error I get is:
imagecopyresized(): supplied argument
is not a valid Image resource in
Thanks in advance
Thanks for all your input, i've changed it to this
$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
$copy = imagecopyresized($imgHolder, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]);
if(!move_uploaded_file($copy, $newname)){
$errors=1;
}
Not getting a PHP log error but its not saving :(
Any ideas?
Thanks again
Result
Following works.
$oldImage = imagecreatefromjpeg($img);
$imageHolder = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($imageHolder, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($imageHolder, $newname, 100);
Thanks for everyones help
imagecopyresized takes an image resource as its second parameter, not a file name. You'll need to load the file first. If you know the file type, you can use imagecreatefromFILETYPE to load it. For example, if it's a JPEG, use imagecreatefromjpeg and pass that the file name - this will return an image resource.
If you don't know the file type, all is not lost. You can read the file in as a string and use imagecreatefromstring (which detects file types automatically) to load it as follows:
$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
$_FILES['image']['tmp_name'] is path not image resource. You have to use one of imagecreatefrom*() functions to create resource.
Here is my implementation of saving a thumbnail picture:
Resize and save function:
function SaveThumbnail($imagePath, $saveAs, $max_x, $max_y)
{
ini_set("memory_limit","32M");
$im = imagecreatefromjpeg ($imagePath);
$x = imagesx($im);
$y = imagesy($im);
if (($max_x/$max_y) < ($x/$y))
{
$save = imagecreatetruecolor($x/($x/$max_x), $y/($x/$max_x));
}
else
{
$save = imagecreatetruecolor($x/($y/$max_y), $y/($y/$max_y));
}
imagecopyresized($save, $im, 0, 0, 0, 0, imagesx($save), imagesy($save), $x, $y);
imagejpeg($save, $saveAs);
imagedestroy($im);
imagedestroy($save);
}
Usage:
$thumb_dir = "/path/to/thumbnaildir/"
$thumb_name = "thumb.jpg"
$muf = move_uploaded_file($_FILES['imgfile']['tmp_name'], "/tmp/test.jpg")
if($muf)
{
SaveThumbnail("/tmp/test.jpg", $thumb_dir . $thumb_name, 128, 128);
}
I use ImageMagick for stuff like that. Look how much simpler it is!
An example from one of my scripts:
$target= //destination path
move_uploaded_file($_FILES['item']['tmp_name'],$target);
$image = new imagick($target);
$image->setImageColorspace(imagick::COLORSPACE_RGB);
$image->scaleImage(350,0);
$image->writeImage($target);
You could then use getImageGeometry() to obtain the width and height.
For example:
$size=$image->getImageGeometry();
if($size['width'] > 300){ //if width greater than
$image->scaleImage(300,0);
}
Also, using scaleImage(300,0) means that ImageMagick automatically calculates the height based on the aspect ratio.
I was working on sth similar. I tried Ghostscript and ImageMagic. They are good tools but takes a but of time to set up. I ended up using 'sips' on a Snow Leopard server. Not sure if it's built in to Linux server but it's the faster solution I have found if you need sth done quick.
function resizeImage($file){
define ('MAX_WIDTH', 1500);//max image width
define ('MAX_HEIGHT', 1500);//max image height
define ('MAX_FILE_SIZE', 10485760);
//iamge save path
$path = 'storeResize/';
//size of the resize image
$new_width = 128;
$new_height = 128;
//name of the new image
$nameOfFile = 'resize_'.$new_width.'x'.$new_height.'_'.basename($file['name']);
$image_type = $file['type'];
$image_size = $file['size'];
$image_error = $file['error'];
$image_file = $file['tmp_name'];
$image_name = $file['name'];
$image_info = getimagesize($image_file);
//check image type
if ($image_info['mime'] == 'image/jpeg' or $image_info['mime'] == 'image/jpg'){
}
else if ($image_info['mime'] == 'image/png'){
}
else if ($image_info['mime'] == 'image/gif'){
}
else{
//set error invalid file type
}
if ($image_error){
//set error image upload error
}
if ( $image_size > MAX_FILE_SIZE ){
//set error image size invalid
}
switch ($image_info['mime']) {
case 'image/jpg': //This isn't a valid mime type so we should probably remove it
case 'image/jpeg':
$image = imagecreatefromjpeg ($image_file);
break;
case 'image/png':
$image = imagecreatefrompng ($image_file);
break;
case 'image/gif':
$image = imagecreatefromgif ($image_file);
break;
}
if ($new_width == 0 && $new_height == 0) {
$new_width = 100;
$new_height = 100;
}
// ensure size limits can not be abused
$new_width = min ($new_width, MAX_WIDTH);
$new_height = min ($new_height, MAX_HEIGHT);
//get original image h/w
$width = imagesx ($image);
$height = imagesy ($image);
//$align = 'b';
$zoom_crop = 1;
$origin_x = 0;
$origin_y = 0;
//TODO setting Memory
// generate new w/h if not provided
if ($new_width && !$new_height) {
$new_height = floor ($height * ($new_width / $width));
} else if ($new_height && !$new_width) {
$new_width = floor ($width * ($new_height / $height));
}
// scale down and add borders
if ($zoom_crop == 3) {
$final_height = $height * ($new_width / $width);
if ($final_height > $new_height) {
$new_width = $width * ($new_height / $height);
} else {
$new_height = $final_height;
}
}
// create a new true color image
$canvas = imagecreatetruecolor ($new_width, $new_height);
imagealphablending ($canvas, false);
if (strlen ($canvas_color) < 6) {
$canvas_color = 'ffffff';
}
$canvas_color_R = hexdec (substr ($canvas_color, 0, 2));
$canvas_color_G = hexdec (substr ($canvas_color, 2, 2));
$canvas_color_B = hexdec (substr ($canvas_color, 2, 2));
// Create a new transparent color for image
$color = imagecolorallocatealpha ($canvas, $canvas_color_R, $canvas_color_G, $canvas_color_B, 127);
// Completely fill the background of the new image with allocated color.
imagefill ($canvas, 0, 0, $color);
// scale down and add borders
if ($zoom_crop == 2) {
$final_height = $height * ($new_width / $width);
if ($final_height > $new_height) {
$origin_x = $new_width / 2;
$new_width = $width * ($new_height / $height);
$origin_x = round ($origin_x - ($new_width / 2));
} else {
$origin_y = $new_height / 2;
$new_height = $final_height;
$origin_y = round ($origin_y - ($new_height / 2));
}
}
// Restore transparency blending
imagesavealpha ($canvas, true);
if ($zoom_crop > 0) {
$src_x = $src_y = 0;
$src_w = $width;
$src_h = $height;
$cmp_x = $width / $new_width;
$cmp_y = $height / $new_height;
// calculate x or y coordinate and width or height of source
if ($cmp_x > $cmp_y) {
$src_w = round ($width / $cmp_x * $cmp_y);
$src_x = round (($width - ($width / $cmp_x * $cmp_y)) / 2);
} else if ($cmp_y > $cmp_x) {
$src_h = round ($height / $cmp_y * $cmp_x);
$src_y = round (($height - ($height / $cmp_y * $cmp_x)) / 2);
}
// positional cropping!
if ($align) {
if (strpos ($align, 't') !== false) {
$src_y = 0;
}
if (strpos ($align, 'b') !== false) {
$src_y = $height - $src_h;
}
if (strpos ($align, 'l') !== false) {
$src_x = 0;
}
if (strpos ($align, 'r') !== false) {
$src_x = $width - $src_w;
}
}
// positional cropping!
imagecopyresampled ($canvas, $image, $origin_x, $origin_y, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h);
} else {
imagecopyresampled ($canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
}
//Straight from Wordpress core code. Reduces filesize by up to 70% for PNG's
if ( (IMAGETYPE_PNG == $image_info[2] || IMAGETYPE_GIF == $image_info[2]) && function_exists('imageistruecolor') && !imageistruecolor( $image ) && imagecolortransparent( $image ) > 0 ){
imagetruecolortopalette( $canvas, false, imagecolorstotal( $image ) );
}
$quality = 100;
$nameOfFile = 'resize_'.$new_width.'x'.$new_height.'_'.basename($file['name']);
if (preg_match('/^image\/(?:jpg|jpeg)$/i', $image_info['mime'])){
imagejpeg($canvas, $path.$nameOfFile, $quality);
} else if (preg_match('/^image\/png$/i', $image_info['mime'])){
imagepng($canvas, $path.$nameOfFile, floor($quality * 0.09));
} else if (preg_match('/^image\/gif$/i', $image_info['mime'])){
imagegif($canvas, $path.$nameOfFile);
}
}

Categories