Thumbnail Script Not Creating Square Images - php

I'm using a thumbnail script that is supposed to create thumbnails for images in my gallery directory. It works great. But one issue. The images are coming out rectangular instead of square. I need the images to come out square. I have the width set to 150 and the height set to 150. I'm not sure what I'm doing wrong. This is a pre-written script I am using that can be found at http://www.foliopages.com/php-photo-gallery-no-database [Not my site, this is the source of the script]
This is the first half of the thumbnail script(there are other functions in between these two parts):
$thumb_width = '150'; // width of thumbnails
$thumb_height = '150'; // height of thumbnails
$extensions = array(".jpg",".png",".gif",".JPG",".PNG",".GIF"); // allowed extensions in photo gallery
// create thumbnails from images
function make_thumb($folder,$src,$dest,$thumb_width) {
$source_image = imagecreatefromjpeg($folder.'/'.$src);
$width = imagesx($source_image);
$height = imagesy($source_image);
$thumb_height = floor($height*($thumb_width/$width));
$virtual_image = imagecreatetruecolor($thumb_width,$thumb_height);
imagecopyresampled($virtual_image,$source_image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
imagejpeg($virtual_image,$dest,100);
}
And the second part:
$thumb = $src_folder.'/thumbs/'.$file;
if (!file_exists($thumb)) {
make_thumb($src_folder,$file,$thumb,$thumb_width);
}

The issue is that you calculate a value for $thumb_height in the line
$thumb_height = floor($height*($thumb_width/$width));
Replace that line with this one to make the thumb square:
$thumb_height=$thumb_width;

Related

PHP resize image to fit within a certain area

I have an image file that is stored within the variable $image I want to resize this image so it would fit within an area of 380px by 380px (so which means that the tallest side of the image must be 380px on the other side smaller than 380px).
Has anyone a suggestion on how to do this?
Thanks
here is what I use to keep it under 800x600
$orig_image = imagecreatefromjpeg($file['tmp_name']);
list($width,$height) = getimagesize($file['tmp_name']);
if(max($width,$height) > 800){
$scale = 800/max($width,$height);
$new_width = floor($width*$scale);
$new_height = floor($height*$scale);
$save_image = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($save_image,$orig_image,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($save_image,self::$FILE_DIRECTORY."$year_month/$fileId.jpg");
$orig_image = $save_image;
$width = $new_width;
$height = $new_height;
}
hopefully you can extrapolate a solution off that.. also not that my $file variable is coming from an uploaded file in the $_FILE array.

Crop Image with jWindowCrop - PHP

I'm using this jQuery plugin to crop images:
http://www.tmatthew.net/jwindowcrop
As you can see, it's really easy to use it on jQuery side, but my problem is with cropping the real image with PHP/GD.
with some goggling, I got:
$targ_w = $targ_h = 150;
$jpeg_quality = 90;
$src = 'demo_files/flowers.jpg';
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
header('Content-type: image/jpeg');
imagejpeg($dst_r, null, $jpeg_quality);
But it's not taking care of zoom ins/zoom outs made by the jQuery plugin, how I should crop the image and save it using this plugin and PHP?
I figured this out, here is my code in case of anyone else has the same question, the cropping will be done by Zebra image class:
http://stefangabos.ro/php-libraries/zebra-image/#documentation
PHP:
// The variables we got from the plugin in upload page:
$x = intval($_POST['x']);
$y = intval($_POST['y']);
$w = intval($_POST['w']);
$h = intval($_POST['h']);
// The img file which we want to crop
$tmp_file = 'path/to/img';
// Now include the Zebra class
include_once('path/to/Zebra_Image.php');
$image = new Zebra_Image();
$image -> preserve_aspect_ratio = true;
$image -> enlarge_smaller_images = true;
$image -> preserve_time = true;
$image -> jpeg_quality = 100;
// Now imagine that the user has selected the area which he want with the plugin, and we also want to make the image out put in a specific size(200*225):
$target_path = 'new/img/path'; // the output img path
$image -> source_path = $tmp_file;
$image -> target_path = $target_path;
$image -> crop($x, $y, $x + $w, $y + $h);
$image -> source_path = $target_path;
$image -> resize(200, 225, ZEBRA_IMAGE_CROP_CENTER);
I am also using jwindowcrop. When you click jwindowcrop's zoom, the w and h changes. (see attached picture)
You have to make sure you used the correct parameters as stated in the php manual
http://www.php.net/manual/en/function.imagecopyresampled.php
In my case, I used imagecopyresized and I could crop the image properly including the zooms
dst_image
Destination image link resource.
src_image
Source image link resource.
dst_x
x-coordinate of destination point.
(in my case the destination image should start from upper left corner)
dst_y
y-coordinate of destination point.
(in my case the destination image should start from upper left corner)
src_x
x-coordinate of source point.
(the x-coordinate returned by the cropping function e.g. crop image from x=231, 231 pixels far from the left edge)
src_y
y-coordinate of source point.
(the x-coordinate returned by the cropping function e.g. crop image from y=706, 706 pixels far from the top edge)
dst_w
Destination width.
(in my case, my new image should have a width of 800px)
dst_h
Destination height.
(in my case, my new image should have a height of 400px)
src_w
Source width.
(when my cropping function zooms, it changes the width and height of the original image)
src_h
Source height.
(when my cropping function zooms, it changes the width and height of the original image)
imagecopyresized(dst_image, src_image, 0, 0, 231, 706, 800, 400, 521, 318);

PHP: Create Thumbnail Image Using X,Y Coordinates

How can I take a 500x500 (or any sized) image that has been uploaded to the server and generate a new image from defined specific x,y coordinates? For example (0,0) to (50,0); (0,50) to (50,50). Rather than resizing an image down to 50x50px, I want to grab the top left part of an image and in a sense "crop" it to use as a thumbnail.
How can I go about doing this in PHP?
You want to use imagecopy. First create an image with the dimensions you want and then use imagecopy to a portion of the source image into the new image:
// use whatever mechanism you prefer to load your source image into $image
$width = 50;
$height = 50;
// Define your starting coordinates in the source image.
$source_x = 20;
$source_y = 100;
$new_image = imagecreatetruecolor($width, $height);
imagecopy($new_image, $image, 0, 0, $source_x, $source_y, $width, $height);
// Now $new_image has the portion cropped from the source and you can output or save it.
imagejpeg($new_image);
http://www.php.net/manual/en/function.imagick-cropthumbnailimage.php#81547
$image = new Imagick($path."test1.jpg");
$image->cropThumbnailImage(160,120); // Crop image and thumb
$image->writeImage($path."test1.jpg");
I've seen a few ways to do this. If you want to generate the thumbs on the fly you could use:
function make_thumb($src,$dest,$desired_width)
{
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height*($desired_width/$width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width,$desired_height);
/* copy source image at a resized size */
imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image,$dest);
}
You can also set the quality parameter in the imagejpeg function.
Or if you want to save the image thumb to a directory I would look at:
http://bgallz.org/270/php-create-thumbnail-images/
or
http://www.imagemagick.org/script/index.php

resizing with php imagick

I am uploading images and resizing them to 300x200 pixels
i'm using the imagick extension and specifically the adaptiveResizeImage() function.
when uploaded using bestfit there is a lot of whitespace on the sides of the image (depending on portrait or landscape).
what i'd rather it did was resize to fill the entire area (no whitespace) and crop the longer length (height or width), ie i'd rather lose some image than have whitespace.
is there an easy way to do this with the imagick extension?
EDIT: I managed to do what i needed, but there has to be a better way
header('Content-type: image/jpeg');
// target sizes
$target_width = 300 ;
$target_height = 100 ;
// create new image
$image = new Imagick('test.jpg');
// get current size and calculate diffences from target sizes
$size = $image->getImageGeometry();
$size['width_diff'] = $target_width/$size['width'] ;
$size['height_diff'] = $target_height/$size['height'] ;
// resize by smallest size
if($size['width_diff']>=$size['height_diff'])
{
$width = $target_width ;
$height = $size['height']*$size['width_diff'] ;
}
else
{
$width = $size['width']*$size['height_diff'] ;
$height = $target_height ;
}
// get offsets
$x = ($width-$target_width)/2 ;
$y = ($height-$target_height)/2 ;
// resize and offset image
$image->adaptiveResizeImage($width, $height) ;
$image->extentImage($target_width, $target_height,-$x,-$y);
// output and clean up
echo $image ;
$image->clear();
$image->destroy();
Maybe you can take a look at the extent flag in the ImageMagick tutorial.
From a quick look it seems the equivalent PHP would be Imagick::ExtentImage
but have not used it from php.

PHP GD imagecreatefromjpeg cant handle big size image?

my project is when im uploading an image automatically my program will create the thumb size.My program work normally if the size of picture about 1024x768 But when i'm uploading image with size 1576x2379 showing error like this:
Allowed memory size of 8388608 bytes exhausted (tried to allocate 1576 bytes)
I'm using method imagcreatefromjpeg().How can I create thumb version from big size image using PHP???
thanks
You have to edit your php.ini
Find the line with Memory limit statement and change its default value for something bigger - for example 128M
For me this problem solved with next peace of code:
first of all you need imagemagick on your server;
and install it also as part of php (php5-imagick)
my part of code (for Smart Image Resizer 1.4.1 )
i found line "$src = $creationFunction($docRoot . $image);"
and replace with
if ( $width >= 1900 )
{
// Read original image and create Imagick object
$thumb = new Imagick($docRoot . $image);
$newX = 1600;
$newY = 1200;
// Scale the image
$thumb->thumbnailImage($newX,$newY);
#$thumb->cropThumbnailImage(600,600);
// make new file-name
$_ext_pos = strrpos($image,'.');
$_image_name_p1 = substr($image, 0, $_ext_pos);
$_image_name_p2 = substr($image, $_ext_pos);
$thumbnailFilename = $_image_name_p1.'_s600'.$_image_name_p2;
// Write the new image to a file
$thumb->writeImage($docRoot . $thumbnailFilename);
$thumb->destroy();
// Read in the original image
$src = $creationFunction($docRoot . $thumbnailFilename);
// reset w-h
$size = GetImageSize($docRoot . $thumbnailFilename);
$width = $size[0];
$height = $size[1];
// Setting up the ratios needed for resizing.
// resize the image (based on height or based on width)
$xRatio = 1;#$maxWidth / $width;
$yRatio = 1;#$maxHeight / $height;
if ($xRatio * $height < $maxHeight)
{ // Resize the image based on width
$tnHeight = ceil($xRatio * $height);
$tnWidth = $maxWidth;
}
else // Resize the image based on height
{
$tnWidth = ceil($yRatio * $width);
$tnHeight = $maxHeight;
}
}
else
{
// Read in the original image
$src = $creationFunction($docRoot . $image);
}
so I replace the "ImageCreateFromJpeg" for large images with imagick-workflow
good luck!

Categories