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!
Related
When user upload AI file then if Dimension of an uploaded file is less than or equal to (180 x 180) then I want to resize this file to increased dimension (277 x 277) and then convert it to JPG. I tried code mentioned below. I tried all ways which are commented in the code.
$image_rs = new Imagick();
$image_rs->readimage(realpath(SOURCE_UPLOAD_PATH.$source_file_name));
if($fileType == "ai"){
$image_rs->setIteratorIndex(0);
}
$dimensions = $image_rs->getImageGeometry();
$width = $dimensions['width'];
$height = $dimensions['height'];
if($width <= 180 && $height <= 180){
//$image_rs->magnifyImage();
$image_rs->setImageFormat($source_file_ext);
$image_rs->scaleImage(277,0);
// $image_rs->adaptiveResizeImage(277,277);
// $image_rs->resizeImage(277,277,\Imagick::FILTER_CATROM, 1, true);
$image_rs->writeImage(realpath(RESIZED_UPLOAD_PATH)."/". $source_file_name);
$image = new Imagick();
$image->readimage(realpath(RESIZED_UPLOAD_PATH.$source_file_name));
if($fileType == "ai"){
$image->setIteratorIndex(0);
}
$dimensions = $image->getImageGeometry();
$width = $dimensions['width'];
$height = $dimensions['height'];
$image->thumbnailImage($width, $height);
$image->writeImage(realpath(CONVERTED_UPLOAD_PATH)."/". $source_file_name_without_ext.".jpg");
}
Issue: By using this code image is resized successfully but resized AI file also becomes blurry. because of Imagick library when resizing the image it converts AI file to jpg. AI file is vector file so there is no chance to become blurry even if we increase dimension. So how I can do this by imagemagick?
I have my own PHP function allowing users to update their profile image.
It all works fine, however, I am having a problem where users can upload any size image they want; i.e: 564 x 346.
I do not want this to happen. I would like the image that they select to upload to be cropped to a 1:1 ratio and centred; i.e: it goes from 564 x 346 to 346 x 346 and centres to the middle of the image.
None of the scripts that I have found seem to work for my site (at least the way I want them to).
This is the code I am currently using to update their avatar. It consists of checking if they have a correct file extension & if the image is less than 256kb in size:
$ext = array('jpg', 'jpeg', 'png');
$file = $_FILES['avatar']['name'];
$fileext = strtolower(end(explode('.', $file)));
$filetmp = $_FILES['avatar']['tmp_name'];
if(!in_array($fileext, $ext)){
$errors[] = 'Please select a valid file type. (JPG, JPEG or PNG)'; }
if($_FILES['avatar']['size'] > 256000){
$errors[] = 'Avatars cannot exceed a 256kb file size.';
}
if(empty($errors)){
updateAvatar($conn, $username, $filetmp, $fileext);
} else if(!empty($errors)){
echo output_errors($errors);
}
if(isset($_SESSION['updateAvat'])){
flash('You have successfully updated your avatar.');
unset($_SESSION['updateAvat']);
}
This is the updateAvatar() function that I have made and is called on line 13:
function updateAvatar($conn, $username, $filetmp, $fileext){
$file = md5(microtime() . $filetmp) . '.' . $fileext;
$filepth = './data/user_data/img/udid/prof/' . $file;
move_uploaded_file($filetmp, $filepth);
if(mysqli_query($conn, "UPDATE users SET profile = '$file' WHERE username = '$username'")){
$_SESSION['updateAvat'] = md5(microtime() . $filetmp);
} else {
$errors[] = 'There was a problem updating your avatar. Please try again.';
}
}
However, this is not enough and does not allow my users profile page to work or look the way it should, I am going for something along the lines of how Google or Twitter do their avatar's.
All help is appreciated.
Cheers.
There is imagecrop() function in php:
$tmpfile = $_FILES['avatar']['tmp_name'];
switch ($fileext) {
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($tmpfile);
break;
case 'png':
$image = imagecreatefrompng($tmpfile);
break;
default:
die("wtf is this extension??");
}
list($w, $h) = getimagesize($imgfile);
// get width $w and height $h for the image
if ($w < $h) // then keep the width and scale the height
$image = imagecrop($image, array(
"x" => 0,
"y" => ($h - $w) / 2,
"width" => $w,
"height" => $w
));
else if ($h < $w) // then keep the height and scale the width
$image = imagecrop($image, array(
"x" => ($w - $h) / 2,
"y" => 0,
"width" => $h,
"height" => $h
));
I haven't tried this code but I'm quite sure it's right. Try it and tell me if it doesn't work.
UPDATE:
Then you can save the resource $image with imagejpeg() or imagepng() to save the images with the same extensions so you won't have problems with the database:
imagejpeg($image, "/path/to/your/image.jpg");
This is an explanation of how the process works, this is not a copy
and paste code but a description of the logic processes you need to
understand to resize images in PHP
You want to be using PHP imagecopyresampled to take the original image and then work out mathematically the margins to make it a square and then resize that square.
Process:
Uploaded image is checked is a genuine image and genuine filetype.
Using imagecreatefromjpeg (or similar ones for PNG etc.) you can import the uploaded image file resource.
Using getimagesize you can find the dimensions, you can then find the aspect ratio of the image.
If the aspect ratio is > 1 then width is larger than height so you then need to assume a square the same size as height (as it's the smaller value). For example:
.
Width: 450
Height: 300
Ratio = 450/300 = 3/2 = 1.5 = >1
If the aspect ratio is <1 then the height is larger than the width.
So you want a square that is 300x300 so you need to cut off the excess 150 from the width. This is called the Margin, and to centre the image you need to cut off half of the margin from each side so:
.
$fullMargin = (width- height)
$margin = $fullMargin/2
Just before resizing and saving the new image, a new image resource should be generated:
.
$max_width = $max_height = 256;
$smallestDimensionValue = $originalHeight //(in this example);
$newImage = imagecreatetruecolor($max_width,$max_height);
$sourceHeight = $originalHeight;
$sourceWidth = $originalWidth - $fullMargin;
Finally, Image copy and resample and the output should be as intended:
.
imagecopyresampled($nwImage, $sourceImage, 0,0,$margin,0,$max_width,$max_height,$sourceWidth,$sourceHeight);
PHP scales the image in the imagecopyresampled automatically, but to understand it, Once you've established the Margin size for the full size image you then have to multiply the margin by the same value as the width and the height to scale the overall image to the appropriate size.
Say for example you want the avatar to have a maximum size of 256px then:
maximum_allowed_width / original_width = scale_factor.
So for the example above this is:
256 / 450 = 0.56888888
This is the value the margin value defined above, as well as the height and width of the destination image, is multiplied by.
I have a small PHP script which converts image files to thumbnails. The uploader I have has a 100MB max, which I'd like to keep.
The problem is, when opening the file, GD decompresses it, causing it to be huge and make PHP run out of memory (Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64000 bytes)). I don't want to increase my memory any further than this allowed size.
I don't care about the image, I can just make it show a default thumbnail, that's fine. But I do need a way to catch the error imagecreatefromstring(file_get_contents($file)) spawns when the image is too big.
Since the error spawned is fatal, it cannot be try-catched, and since it loads it in one command, I can't keep looking after it to make sure it's not approaching the limit. I need a way to calculate how big the image will be before trying to process it.
Is there a way to do this? filesize wouldn't work as it gives me the compressed size...
My code is as follows:
$image = imagecreatefromstring(file_get_contents($newfilename));
$ifilename = 'f/' . $string . '/thumbnail/thumbnail.jpg';
$thumb_width = 200;
$thumb_height = 200;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// Image is wider than thumbnail.
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// Image is taller than thumbnail.
$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, $ifilename, 80);
Tried looking at the original image size before re-sizing? Maybe multiplying it by a set % based of average format compression?
$averageJPGFileRatio = 0.55;
$orgFileSize = filesize ($newfilename) * 0.55;
and looking at that before doing any work?
Secondary idea
Calculate it like this: width * height * 3 = filesize
The 3 is for the red, green, and blue values if you're working with images with an alpha channel use 4 instead of 3. This should give you a VERY close estimation of the bitmap size. Does not take into account header info, but that should be negligible at a few bytes.
I have made two GIFs to explain what I am trying to do. Where the grey border is the dimensions I am after (700*525). They are at the bottom of this question.
I want for all images that are larger than the given width and height to scale down to the border (from the centre) and then crop off the edges. Here is some code I have put together to attempt this:
if ($heightofimage => 700 && $widthofimage => 525){
if ($heightofimage > $widthofimage){
$widthofimage = 525;
$heightofimage = //scaled height.
//crop height to 700.
}
if ($heightofimage < $widthofimage){
$widthofimage = //scaled width.
$heightofimage = 700;
//crop width to 525.
}
}else{
echo "image too small";
}
Here are some GIFs that visually explain what I am trying to achieve:
GIF 1: Here the image proportions are too much in the x direction
GIF 2: Here the image proportions are too much in the y direction
image quality comparison for #timclutton
so I have used your method with PHP (click here to do your own test with the php) and then compared it to the original photo as you can see there is a big difference!:
Your PHP method:
(source: tragicclothing.co.uk)
The actual file:
(source: mujjo.com)
The below code should do what you want. I've not tested it extensively but it seems to work on the few test images I made. There's a niggling doubt at the back of mind that somewhere my math is wrong, but it's late and I can't see anything obvious.
Edit: It niggled enough I went through again and found the bug, which was that the crop wasn't in the middle of the image. Code replaced with working version.
In short: treat this as a starting point, not production-ready code!
<?php
// set image size constraints.
$target_w = 525;
$target_h = 700;
// get image.
$in = imagecreatefrompng('<path to your>.png');
// get image dimensions.
$w = imagesx($in);
$h = imagesy($in);
if ($w >= $target_w && $h >= $target_h) {
// get scales.
$x_scale = ($w / $target_w);
$y_scale = ($h / $target_h);
// create new image.
$out = imagecreatetruecolor($target_w, $target_h);
$new_w = $target_w;
$new_h = $target_h;
$src_x = 0;
$src_y = 0;
// compare scales to ensure we crop whichever is smaller: top/bottom or
// left/right.
if ($x_scale > $y_scale) {
$new_w = $w / $y_scale;
// see description of $src_y, below.
$src_x = (($new_w - $target_w) / 2) * $y_scale;
} else {
$new_h = $h / $x_scale;
// a bit tricky. crop is done by specifying coordinates to copy from in
// source image. so calculate how much to remove from new image and
// then scale that up to original. result is out by ~1px but good enough.
$src_y = (($new_h - $target_h) / 2) * $x_scale;
}
// given the right inputs, this takes care of crop and resize and gives
// back the new image. note that imagecopyresized() is possibly quicker, but
// imagecopyresampled() gives better quality.
imagecopyresampled($out, $in, 0, 0, $src_x, $src_y, $new_w, $new_h, $w, $h);
// output to browser.
header('Content-Type: image/png');
imagepng($out);
exit;
} else {
echo 'image too small';
}
?>
Using Imagick :
define('PHOTO_WIDTH_THUMB', 700);
define('PHOTO_HEIGHT_THUMB', 525);
$image = new Imagick();
$image->readImage($file_source);
$width = $image->getImageWidth();
$height = $image->getImageHeight();
if($width > $height){
$image->thumbnailImage(0, PHOTO_HEIGHT_THUMB);
}else{
$image->thumbnailImage(PHOTO_WIDTH_THUMB, 0);
}
$thumb_width = $image->getImageWidth();
$thumb_height = $image->getImageHeight();
$x = ($thumb_width - PHOTO_WIDTH_THUMB)/2;
$y = ($thumb_height - PHOTO_HEIGHT_THUMB)/2;
$image->cropImage(PHOTO_THUMB_WIDTH, PHOTO_THUMB_HEIGHT, $x, $y);
$image->writeImage($thumb_destination);
$image->clear();
$image->destroy();
unlink($file_source);
I have used GD library to accomplish the resize. Basically what I did is, I calculated the image dimension and then resized the image to dimension 700x525 from the center.
<?php
/*
* PHP GD
* resize an image using GD library
*/
//the image has 700X525 px ie 4:3 ratio
$src = 'demo_files/bobo.jpg';
// Get new sizes
list($width, $height) = getimagesize($src);
$x = 0;
$y = 0;
if($width < $height){
$newwidth = $width;
$newheight = 3/4 * $width;
$x = 0;
$y = $height/2 - $newheight/2;
}else{
$newheight = $height;
$newwidth = 4/3 * $height;
$x=$width/2 - $newwidth/2;
$y=0;
}
$targ_w = 700; //width of the image to be resized to
$targ_h = 525; ////height of the image to be resized to
$jpeg_quality = 90;
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$x,$y,$targ_w,$targ_h,$newwidth,$newheight);
header('Content-type: image/jpeg');
imagejpeg($dst_r,null,$jpeg_quality);
exit;
?>
i used http://phpthumb.sourceforge.net to have a beutiful solution also with transparent curved edges.
this is an alternative route to solution, might suit someone's need with little configuration.
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.