I really need another brain here. I have a script that makes three different versions of an image, and saves them in different folders (large, medium, thumbnail). It resizes them and puts them in their folders, but the large one is not readable (the other two are). It has nothing to do with the folder, so I'm stuck...
Here's my (simplified) code:
<?php
$target_folder = "images/";
$uploads_dir = $target_folder;
$upload_image = $_FILES['Filedata']['tmp_name'];
$id = $_POST['post_id'];
$image_name = $id . "." . time(); //this just generates the image name
$large_name = $target_folder . "large/" . $image_name . ".jpg";
$medium_name = $target_folder . "medium/" . $image_name . ".jpg";
$small_name = $target_folder . "small/" . $image_name . ".jpg";
list($width, $height) = getimagesize($upload_image); //width/height of original image
$medium_newwidth = $width * 0.60; //scales image down to 60%
$medium_newheight = $height * 0.60;
$small_newwidth = $width * 0.20; //scales image down to 20%
$small_newheight = $height * 0.20;
$large = imagecreatefromjpeg($upload_image); //here's where I think the problem might be
$medium = imagecreatetruecolor($medium_newwidth, $medium_newheight);
$small = imagecreatetruecolor($small_newwidth, $small_newheight);
imagecopyresized($medium, $large, 0, 0, 0, 0, $medium_newwidth, $medium_newheight, $width, $height);
imagecopyresized($small, $large, 0, 0, 0, 0, $small_newwidth, $small_newheight, $width, $height);
imagejpeg($medium, $medium_name, 100);
imagejpeg($small, $small_name, 100);
rename($upload_image, $large_name);
?>
Any ideas?
Thanks!
You should use move_uploaded_file instead of rename if you want to use the original upload. Alternatively you could save the large image like you are doing with the other ones:
imagejpeg($large, $large_name, 100);
Related
Here is my code :
foreach($_FILES as $key=>$photo){
if ($check[$key] == 'OK'){
$path = '../images/chevaux/' . $_POST['horseName'] . '_' . $horseID . '/' . $key . '.' . $file_extension;
$folder = '../images/chevaux/' . $_POST['horseName'] . '_' . $horseID;
if (!is_dir($folder))
{
mkdir($folder);
}
$filename = $photo['tmp_name'];
$percent = 4.08;
list($width, $height) = getimagesize($filename);
$newwidth = $width / $percent;
$newheight = $height / $percent;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename, $path);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, $path);
//....
All is working fine. Except that my pictures are completely black. The aim is to reduce their width and height by dividing them by 4.08. Know that uploaded image have width equal to 3264 px and height equal to 2448 px. Maybe it is too much?
imagecreatefromjpeg() takes only 1 parameter, the path to the filename (Either local path or URL). You're giving it two parameters.
I want to rotate my crop image and then save. But its not working.
<?php
$newNamePrefix = $newname;
$manipulator = new ImageManipulator($_FILES['blogo']['tmp_name']);
$width = $manipulator->getWidth();
$height = $manipulator->getHeight();
$min= min($width ,$height);
$max= max($width ,$height);
$n=$min/800;
$d=$max/$n;
$newImage = $manipulator->resample($d,$d);
$newImage2 = $newImage->crop(0, 0, 800, 600);
// saving file to uploads folder
$manipulator->save('uploads/stores/'.$formData['url'] .'/'. $newNamePrefix.$fileExtension );
return $newname.$fileExtension;
?>
I tried this below code but it didn't work.
$degrees = 90;
$filename = $newImage2;
$source = imagecreatefromjpeg( $filename );
$rotate = imagerotate( $source, $degrees, 0 );
$fileName = 'uploads/stores/'.$formData['url'] .'/'. $newNamePrefix.$fileExtension;
// Output
imagejpeg( $rotate, $fileName, 100 );
If i use $filename = $_FILES['blogo']['tmp_name'] it works but if i use $filename = $newImage2; then it doesn't.
I am doing this first time so I have no idea whats the right way to do it.
It is because $_FILES['blogo']['tmp_name'] is a valid name whereas $newImage2 is not... Try to do something like:
echo $newImage2;
And you will understand what I mean.
I've been building a gallery in php as a learning exercise and also because I'll need one to display my wedding photos in a couple of months.
I have written a function that processes the images. It looks to see what images are in the original folder and generates a thumbnail and preview for each. It's worked fine up until now as I've been testing with small batches of images.
Now I have 24 images in the original folder it only process the first 18. I don't get any errors displayed it just seems to stop.
Function
function processImgs($previewWidth, $thumbWidth, $thumbPath, $previewPath, $originalPath) {
$images = glob($originalPath."*");
foreach($images as $image){
$path_parts = pathinfo($image);
// setup filenames
$thumbFile = $thumbPath . "thumb_" . $path_parts['filename'] . ".jpg";
$previewFile = $previewPath . "preview_" . $path_parts['filename'] . ".jpg";
// calculate new images sizes
$size = getimagesize($image);
$originalWidth = $size['0'];
$originalHeight = $size['1'];
$previewHeight = $originalHeight / ($originalWidth / $previewWidth);
$thumbHeight = $originalHeight / ($originalWidth / $thumbWidth);
//create new images
$original = imagecreatefromjpeg($image);
$preview = imagecreatetruecolor($previewWidth, $previewHeight);
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($preview, $original, 0, 0, 0, 0, $previewWidth, $previewHeight, $originalWidth, $originalHeight);
imagecopyresampled($thumb, $original, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $originalWidth, $originalHeight);
// save images to their new folders
imagejpeg($preview, $previewFile);
imagejpeg($thumb, $thumbFile);
imagedestroy($original);
imagedestroy($preview);
imagedestroy($thumb);
// give some feedback when the script runs
if (file_exists($thumbFile) && file_exists($previewFile)) {
echo ". " . $path_parts['filename'] . " processed successfully.<br>";
} else {
echo ". " . $path_parts['filename'] . " <strong>failed.</strong><br>";
}
}
}
I call the function like this
processImgs(1200, 400, "thumb/", "preview/", "original/");
I don't know why it doesn't work with more files. Is there a time limit that I could be hitting? It does take a while. I'm currently building this locally on MAMP.
UPDATE
Mike was correct, the function was timing out. His solution is much more suitable.
I have now created a script that does what he suggested and it has solved the problem. Thanks for the help. I've added the script below in case it's of use to others.
$newWidth = 400;
if (isset($_GET['img'])) {
$img = $_GET['img'];
$filename = $img . ".jpg";
if (file_exists($filename)) {
$fp = fopen($filename, 'rb');
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($filename));
fpassthru($fp);
exit;
} else {
$originalFile = "../original/" . $filename;
$original = imagecreatefromjpeg($originalFile);
$size = getimagesize($originalFile);
$newHeight = $size['1'] / ( $size['0'] / $newWidth);
$new = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($new, $original, 0, 0, 0, 0, $newWidth, $newHeight, $size['0'], $size['1']);
imagejpeg($new, $filename);
imagedestroy($original);
imagedestroy($new);
$fp = fopen($filename, 'rb');
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($filename));
fpassthru($fp);
}
}
I'm kind of a beginner in PHP and here's what I'm trying to do:
Recieve an uploaded image and move it to temp/ folder
Examine it and decide whether it needs to be resized or not (it needs, if it's not 135px wide)
If needs to be resized, resize it and save as results/{number}_cover.jpg
If it doesn't, copy an uploaded image right away to the same place, without resizing
And here's my code:
$target_path = "temp/";
$target_path = $target_path . basename($_FILES["file" . $a]["name"]);
move_uploaded_file($_FILES["file" . $a]["tmp_name"], $target_path);
list($current_width, $current_height, $type, $attr) = getimagesize($target_path);
if($current_width != 135) {
$filename = $a . "_cover.jpg";
$result_image = "results/" . $filename;
$writing = fopen($result_image, 'w');
$scale = (135 / $current_width);
$new_width = 135;
$new_height = $current_height * $scale;
$result_image = imagecreatetruecolor($new_width, $new_height);
$current_image = imagecreatefromjpeg($target_path);
imagecopyresampled($result_image, $current_image, 0, 0, 0, 0, $new_width, $new_height, $current_width, $current_height);
imagejpeg($result_image, null, 100);
fclose($writing);
} else {
$target_path = "results/";
$target_path = $target_path . $a . "_cover.jpg";
move_uploaded_file($_FILES["file" . $a]["tmp_name"], $target_path);
}
However, this is what this code does for me:
1. If an image needs resizing, it just give me an image data to the browser instead of saving it to the file
2. If it doesn't need resizing, nothing happens.
What am I doing wrong?
Thanks in advance for your help!
I have the following in place for all my images in /images/500x500/. I need to resize the images to 250x250 and have them go into the /images/250x250/ with the same filename.
It doesn't create the new image in the new directory, rather it replaces the large in the same directory?
<?php
$files = glob("*.{png,jpg,jpeg}", GLOB_BRACE);
foreach ($files as $file)
{
// get the image size
$imagesize = getimagesize($file);
$width_orig = $imagesize[0];
$height_orig = $imagesize[1];
$dst_w = 250;
if($width_orig != $dst_w)
{
$dst_h_multiplier = $dst_w / $width_orig;
$dst_h = $dst_h_multiplier * $height_orig;
$dst = imagecreatetruecolor($dst_w, $dst_h);
$image = imagecreatefromjpeg($file);
imagecopyresampled($dst, $image, 0, 0, 0, 0, $dst_w, $dst_h ,$width_orig, $height_orig);
imagejpeg($dst, $outputFile, 100);
$outputFile =
realpath(
pathinfo($file, PATHINFO_DIRNAME)
. '/../250x250/'
) . pathinfo($file, PATHINFO_BASENAME);
}
}
?>
$outputFile =
realpath(
pathinfo($file, PATHINFO_DIRNAME)
. '/../'
) . pathinfo($file, PATHINFO_BASENAME);
First, you need the path, which you can get with realpath(), along with pathinfo(). Then, just append a .. and the file name.
http://www.php.net/manual/en/function.pathinfo.php
http://php.net/manual/en/function.realpath.php
Also, you might not need realpath(). I haven't tested.