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.
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'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!
//Your Image
$imgSrc = $_GET['f'];
list($width, $height) = getimagesize($imgSrc);
list($root) = explode('httpdocs', __FILE__);
$root = $root.'httpdocs';
$savePath = $root.'/_m/chacheImages/';
$savePath = '../cacheImages/';
$imgName = basename($imgSrc);
list($imgName) = explode('.', $imgName);
$fileName = $savePath.$imgName.'.jpg';
if($width > 0 && $height > 0){
$thumbSize = 100;
$img_p = imagecreatetruecolor($width, $height);
$img = imagecreatefrompng($imgSrc);
imagecopyresampled($img_p, $img, -10, 0, 0, 10, $width+30, $height, ($width), ($height-20));
//header('Content-type: image/png');
imagejpeg($img_p, $fileName);
imagedestroy($img_p);
}
I can't seem to get this to work. If i delete $fileName at imagejpeg it shows the image correctly.
But it won't save the images. What am I doing wrong?
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);