How to move image to new folder? - php

As the title says ..How to move/rename image to new folder?
I have this so far and the new image is resized/cropped but it doesn't move to "new/" folder:
$in_filename = '4csrWqu9ngv.jpg';
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 65;
$new_width = $width;
$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header('Content-Type: image/jpeg');
imagejpeg($new_image);
$move_new = imagejpeg($new_image);
rename($move_new, 'new/' . $move_new);
As always any help is appreciated :)

You had few mistakes in your code. Output of imagejpeg is a boolean, so your rename always failed. You also never saved resized image. You have to use 2nd parameter of imagejpeg and provide proper filename of new image. Also, make sure directory new exists, else rename will fail.
Fixed code:
$in_filename = '4csrWqu9ngv.jpg';
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 65;
$new_width = $width;
$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
/* Uncomment in case you want it also outputted
header('Content-Type: image/jpeg');
imagejpeg($new_image);
*/
imagejpeg($new_image, $in_filename);
rename($in_filename, 'new/' . $in_filename);

Does the "new" folder exist? If not, you need to create it at first using mkdir.

Related

How to save an image to directory after resize in php?

I am using the GD libary in php to resize uploaded images which works fine but I just can't figure out how to save the resized image to a directory. How do I go about this? I don't seem to get any errors.
<?php
// The file
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$filename = 'test.jpg';
$percent = 0.5;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$filename = $_FILES['photoimg']['tmp_name'];
// 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);
//This is my attempt to save the image that does not work
if (move_uploaded_file($image, "memberFiles/saved_image.jpg")) {
}
}
?>
Just use:
imagejpeg($image_p, 'some/other/existing/directory/result.jpg');

Resize image while uploading to amazon s3

I need to resize image to 150 x 150 px and then upload it to Amazon S3
Following is the code:
$image = $_FILES["userImage"]["name"];
$fileTempName = $_FILES['userImage']['tmp_name'];
$new_width = 150;
$new_height = 150;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromstring(file_get_contents($fileTempName));
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, imagesx($image), imagesy($image));
$newFielName = tempnam(sys_get_temp_dir(), "tempfilename");
imagepng($image_p, $newFielName, 9);
$s3 = new S3(awsAccessKey, awsSecretKey);
//move the file
if ($s3->putObjectFile($fileTempName, "urimages", $newFielName, S3::ACL_PUBLIC_READ)) {
$image_link = 'https://s3-us-west-2.amazonaws.com/urimages/' . $newFielName . '';
$this->Product->saveField('image', $image_link);
}
Following is the link which i receive upon uploading : https://s3-us-west-2.amazonaws.com/urimages/C:/Users/LS/AppData/Local/Temp/9102.tmp
Could be you please help me debug the code
i think issue in path. Please create folder on s3 and make a valid
path according to that folder
https://s3-us-west-2.amazonaws.com/urimages/C:/Users/LS/AppData/Local/Temp/9102.tmp
Example :- Resampling an image proportionally
<?php
// The file
$filename = 'test.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$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, null, 100);
?>

PHP image resize isn't working

I found the following script from PHP site to half the size of the image. I use the database to fetch the link of the image. Other things are working correctly which means there isn't any kind of error anywhere except this one.
echo "<img src='".// File and new size
$filename = '$row["image"]';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
"'>"
Error:
The whole page is destroyed and there is a broken link of an image.
Hope you guys will help me!
It works after you drop those lines:
echo "<img src='".// File and new size
"'>"
header does it for you, informs that there is jpg coming, no need to echo image tag.
The other solution is to remove this line:
header('Content-Type: image/jpeg');
And create new file, then use it as a source of an image:
// Output
$new_filename = 'new_image.jpg';
imagejpeg($thumb,$new_filename);//saves new image to a file, instead of outputting it to the screen
echo "<img src='$new_filename'>";
Try this it works
<?php
$filename = '$row["image"]';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
echo "<img src='".imagejpeg($thumb)."'>";
?>

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.

How can I cut an image from the bottom using PHP?

I want to take out the text in the bottom of an image. How can I cut it from bottom ...say 10 pixels to cut from bottom.
I want do this in PHP. I have lots of images that have text in the bottom.
Is there a way to do it?
Here you go.
To change the name of the image, change $in_filename (currently 'source.jpg'). You can use URLs in there as well, although obviously that will perform worse.
Change the $new_height variable to set how much of the bottom you want cropped.
Play around with $offset_x, $offset_y, $new_width and $new_height, and you'll figure it out.
Please let me know that it works. :)
Hope it helps!
<?php
$in_filename = 'source.jpg';
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 15;
$new_width = $width;
$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header('Content-Type: image/jpeg');
imagejpeg($new_image);
?>
You may use the GD Image Library to manipulate images in PHP. The function you're looking for is imagecopy(), which copies part of an image onto another. Here's an example from PHP.net that does roughly what you describe:
<?php
$width = 50;
$height = 50;
$source_x = 0;
$source_y = 0;
// Create images
$source = imagecreatefromjpeg('source.jpg');
$new = imagecreatetruecolor($width, $height);
// Copy
imagecopy($source, $new, 0, 0, $source_x, $source_y, $width, $height);
// Output image
header('Content-Type: image/jpeg');
imagejpeg($new);
?>
To crop the source image, change the $source_x and $source_y variables to your liking.

Categories