Scenario : Earlier I was reading the url of image directly and resized the image into 4 different size . But I had execution time out . Now I read the url and copy it into a temporary folder and pass the images in local temporary folder to imagecreatefromjpeg().
protected static function saveImage($row,$url){
$percent = 1.0;
$imagethumbsize = 200;
$db = PJFactory::getDbo();
$details = $db->getImageDetails();
$max = sizeof($details);
$tempfilename = "C:".DS."xampp".DS."htdocs".DS."opg-uat".DS."img".DS."temp".DS.$row['CategoryID'].".jpg";
$tempcopy = copy($url,$tempfilename);
foreach ($details as $array) {
$new_width=$array[2];
$new_height=$array[3];
$newfilename = "C:".DS."xampp".DS."htdocs".DS."opg-uat".DS."img".DS."c".DS.$row['CategoryID']."-".$array[1].".jpg";
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($tempfilename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $newfilename);
}
}
Error : The images are correctly being saved in temp folder . But in the destination folder images of all sizes are created but the image looks only black . (Not getting the actual image) . I guess there is some problem with the file reading from local .
Any idea ?
Thanks in advance.
If you are trying to read files other than JPG files, it will return only black images,
So while reading images, check which filetype your file really has and then call the correct function between the three (jpeg, png, gif).
Do you set $width and $height anywhere?
$width = imagesx($image);
$height= imagesy($image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Related
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);
?>
I need resize Image from FTP like /images/1_2.jpg. I need to load it to imagecreatefromjpeg();. Now my script looks like this.
$file = "images/1_2.jpg";
$imagesize = getimagesize($file);
$img = imagecreatefromjpeg($file);
$width = 70;
$height = 100;
$img2 = imagecreatetruecolor($width, $height);
imagecopyresampled($img2, $img, 0, 0, 0, 0, $width, $height, $imagesize[0], $imagesize[1]);
imagejpeg($img2, "images_mini/1_2.jpg");
My problem is that images don't upload to FTP, its shows me everything is ok but i can't find images on FTP.
Sorry I have it, just only bad converted variable, i using cross languages now for application so it is dificult to check everythink. Thank you anyway.
This question already has answers here:
PHP upload and resize image
(4 answers)
Closed 9 years ago.
I am currently using this code so users can upload images to my site
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg','.JPG','.PNG','.BIF','.GIF','.JPEG'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; // filesize in BYTES (currently 0.5MB).
$uploadpath = "/home/path/to/file/files/avatars/$_SESSION[user]";
$posted9 = hash('md5',$_SESSION["user"]).$ext; /* not for security mind you. */
// Upload the file to your specified path.
$f = file_put_contents(
$upload_path . $posted9,
file_get_contents('php://input')
);
if($f)
$m="Avatar updated<br/>"; // It worked.
else
$m="There was an error during the file upload. Please try again."; // It failed :(.
However, when the images are rezised to fit inside the comments on my website, they become distorted and lose their quality.
How using PHP can I resize the images so that the quality is maintained?
<?php
// Url of the file
$url = "/homepages/0/d502303335/htdocs/foo.jpg";
// Set a maximum height and width, I used the ones on your picture
$width = 49;
$height = 47;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($url);
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output, You can use another url, In my case i just overwrite the picture
imagejpeg($image_p, $url);
?>
Here's the result:
http://www.filmgratuiti.org/varie/cenBqHxMD90Q2.jpg
Much more better then your's
So basically after you have uploaded the file you run my code and you are good to go
public function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
http://us3.php.net/imagecopyresampled
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 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