delete the images imagejpeg creates - php

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/';

Related

resize or crop base64 images using php

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);
?>

Make a move_uploaded_file after imagecopyresampled in PHP

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.

Image thumbnail not generating properly using PHP

I have writen a script in PHP to upload an image.
To the point, my goal is to upload and to send 2 images to the server, 1 the original and 1 is the thumbnail. My script works, but is not perfect.
This is my script
<?php
//this is script for get data type file
$acak = rand(000000,999999);// for random
$lokasi_file = $_FILES['fupload']['tmp_name'];
$nama_file = $_FILES['fupload']['name'];
$nama_file_acak = $acak.$nama_file;
$ukuran_file = $_FILES['fupload']['size'];
$tipe_file = $_FILES['fupload']['type'];
$direktori = "fkendaraan/$nama_file_acak";
$uplod = move_uploaded_file($lokasi_file,"$direktori"); //to move image from local to the server folder
//to handle uplod thumbnail image
$img = imagecreatefromjpeg($direktori);
$width = imagesx($img);
$height = imagesy($img);
$new_width = 200;
$new_height = ($new_width/$width) * $height;
$tmp_img = imagecreatetruecolor( $width, $height );
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
//imagecopyresized( $tmp_img, $img, 200, 200, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, $direktori."thumb-".$nama_file_acak );
imagedestroy($tmp_img);
imagedestroy($direktori);
//---------------------------------------------------------------
//I have no Problem with query and database, it works fine
$sql = "";
$query = mysql_query($sql);
?>
This can run but not perfect because the result like this
Any one can help me to fix this? Im very nubie in php
Try changing this:
$tmp_img = imagecreatetruecolor( $width, $height );
To this:
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
Anyway I would recommend you to make use of some classes for these tasks such as:
Shiege Iseng Resize Class.
But of course, if you are trying to learn with this, that's ok :)

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);

Resize image with PHP to new directory

I really don't know what's wrong here and why it isn't recreating the new image to a new directory. Please help! The image isn't being created for some reason. I'm coding everything in Microsoft's WebMatrix.
public static function imgResize($imgdir){
list($width, $height) = getimagesize($imgdir);
$ratio = $width/$height;
$new_height = 90;
$new_width = round($new_height * $ratio);
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_image = imagecreatefromjpeg($imgdir);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($new_image, "gal/newimages/", 100);
}
imagejpeg needs a filename, not a directory. Try:
imagejpeg($new_image, "gal/newimages/" . basename($imgdir), 100);
also, does the folder exist? If not, you need to create it first.

Categories