I'm uploading images from the pc to a server using this php code
$ImageToLoad=mysql_real_escape_string($_POST['image_attached']);
if($ImageToLoad){
$token=$token;//variable
$ImageToLoad = str_replace('data:image/png;base64,', '', $ImageToLoad);
$ImageToLoad = str_replace('data:image/jpeg;base64,', '', $ImageToLoad);
$ImageToLoad = str_replace(' ', '+', $ImageToLoad);
$fileData = base64_decode($ImageToLoad);
$destino_path="/images/$token/image.png";
file_put_contents($destino_path, $fileData);
}
It works ok.
PROBLEM
I need to know how to resize it/crop them before to store the image in the server. Otherwise it keeps the same size (problem when huge image)
Resizing images can be is a costly operation and should be handled by a specialized library. In the past, the default pointer was ImageMagick's Imagick::resizeImage. However, this package does not work for everybody and other solutions have emerged in the meantime.
I suggest gumlet's php-image-resize. Resizing a base64 encoded image can be as simple as that:
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
$image->scale(50);
$image->save('image.jpg');
If we want to create a temp image from an image file for resizing, we can use
imagecreatefromjpeg($filename);
or
imagecreatefrompng($filename);
If we want to create a temp image from blob we can use
imagecreatefromstring($blob);
imagecreatefromstring()
So, give this a try:
<?php
$filename = 'folder_name/resized_image.png'; // output file name
$im = imagecreatefromstring($fileData);
$source_width = imagesx($im);
$source_height = imagesy($im);
$ratio = $source_height / $source_width;
$new_width = 300; // assign new width to new resized image
$new_height = $ratio * 300;
$thumb = imagecreatetruecolor($new_width, $new_height);
$transparency = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefilledrectangle($thumb, 0, 0, $new_width, $new_height, $transparency);
imagecopyresampled($thumb, $im, 0, 0, 0, 0, $new_width, $new_height, $source_width, $source_height);
imagepng($thumb, $filename, 9);
imagedestroy($im);
?>
Related
I'm trying to resize image in PHP.
But after all this work how can I use the move_uploaded_file function to move the tmp_file file to it final directory ?
Here's my code:
$image = imagecreatefromjpeg($_FILES['REG_Image']['tmp_name']);
// Target dimensions
$max_width = 1014;
$max_height = 768;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
if($old_width > $max_height) {
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
}
Thanks.
You can use imagejpeg instead after
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width,$new_height, $old_width, $old_height);
imagejpeg($new, "/path/to/image.jpeg");
this will save the image to the new path that you have specified as image.jpeg. You can use imagepng as well if you want a PNG image as output.
I am making thumbnails and for some reason, my output is the correct size, but always black. I saw another Stack Overflow post on a similar topic but in his case, he was passing parameters incorrectly.
I am capturing an image from a video camera and then using this code:
$data = base64_decode($data); // the data will be saved to the db
$image = imagecreatefromstring($data); // need to create an image to grab the width and height
$img_width = imagesx($image);
$img_height = imagesy($image);
// calculate thumbnail size
$new_height = 100;
$new_width = floor( $img_width * ( 100 / $img_height ) );
// create a new temporary image
$new_image = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
$url = IMGDIR.$imgname;
$thumburl = IMGDIR."thumb/".$imgname;
// save image and thumb to disk
imagepng($image,$url);
imagepng($new_image,$thumburl);
The result I get is both files saved to the proper directories, both the proper size, but the thumbnail is all black. There must be something simple I am missing. Any ideas?
Remember that PNG files have alpha channels. So be sure to use imagealphablending and imagesavealpha. Here they are integrated into your code.
$data = base64_decode($data); // the data will be saved to the db
$image = imagecreatefromstring($data); // need to create an image to grab the width and height
$img_width = imagesx($image);
$img_height = imagesy($image);
// calculate thumbnail size
$new_height = 100;
$new_width = floor( $img_width * ( 100 / $img_height ) );
// create a new temporary image
$new_image = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
$url = IMGDIR . $imgname;
$thumburl = IMGDIR . "thumb/" . $imgname;
// Set the image alpha blending settings.
imagealphablending($image, false);
imagealphablending($new_image, false);
// Set the image save alpha settings.
imagesavealpha($image, true);
imagesavealpha($new_image, true);
// save image and thumb to disk
imagepng($image,$url);
imagepng($new_image,$thumburl);
Try saving your image's alpha channel with imagesavealpha and passing true for the 2nd argument
imagesavealpha($image, true);
imagepng($image,$url);
imagesavealpha($new_image, true);
imagepng($new_image,$thumburl);
Is it possible to delete the images that imagejpeg creates I have the images uploading to my Amazon S3 server but the files just pop up in the main directory on my server after its ran.
$new_height = 120;
$width = imagesx($originalImage);
$height = imagesy($originalImage);
$new_width = round(($width * $new_height) / $height);
$imageResized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($imageResized, $originalImage, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$tmp_loc = 'uploads/thumb/';
$tempfilename = tempnam($tmp_loc, $filename);
imagejpeg($imageResized, $filename,100);
imagedestroy($imageResized);
imagedestroy($originalImage);
unlink($tempfilename);
I tried imagedestroy and unlink($tempfilename); but the file remains.
imagejpeg(...) should be outputting to $tempfilename rather than $filename, then you'd be unlinking the right file.
You forget to open the variable $tmp_loc.
Look:
$tmp_loc = uploads/thumb/';
Correct:
$tmp_loc = 'uploads/thumb/';
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/
I am working on improving my Facebook app. I need to be able to resize an image, then save it to a directory on the server. This is the code I have to resize:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
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);
// Output
imagejpeg($image_p, null, 100);
?>
My question is, how would I save this resized image? Would I need to? Is there a way to manipulate the resized image without saving it?
According to the manual on imagejpeg(), the optional second parameter can specify a file name, which it will be written into.
Filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
To skip this argument in order to provide the quality parameter, use NULL.
It's usually a good idea to write the results to disk for some basic caching, so that not every incoming request leads to a (resource intensive) GD call.
function resize($img){
/*
only if you script on another folder get the file name
$r =explode("/",$img);
$name=end($r);
*/
//new folder
$vdir_upload = "where u want to move";
list($width_orig, $height_orig) = getimagesize($img);
//ne size
$dst_width = 110;
$dst_height = ($dst_width/$width_orig)*$height_orig;
$im = imagecreatetruecolor($dst_width,$dst_height);
$image = imagecreatefromjpeg($img);
imagecopyresampled($im, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width_orig, $height_orig);
//modive the name as u need
imagejpeg($im,$vdir_upload . "small_" . $name);
//save memory
imagedestroy($im);
}
it should be work
http://www.php.net/manual/en/function.imagecopyresampled.php#90038