Resize image while uploading to amazon s3 - php

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

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, rename and upload image with PHP

I would like to resize, rename and upload an image with PHP.
So my working script is the following:
$file_name = $_FILES['HOT_Logo']['name'];
$file_tmp_name = $_FILES['HOT_Logo']['tmp_name'];
$file_target = '../../images/hotel-logos/';
$file_size = $_FILES['HOT_Logo']['size'];
// Resize
$ratio = $width/$height;
if($ratio > 1) {
$new_width = 300;
$new_height = 300/$ratio;
}
else {
$new_width = 300*$ratio;
$new_height = 300;
}
$src = imagecreatefromstring(file_get_contents($file_tmp_name));
$dst = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagedestroy($src);
imagepng($dst, $file_target.$file_name);
imagedestroy($dst);
// Rename file
$temp = explode('.', $_FILES['HOT_Logo']['name']);
$newfilename = 'new_img_name.'.end($temp);
// Upload image
if(move_uploaded_file($_FILES['HOT_Logo']['tmp_name'], $file_target.$newfilename)) {
...
}
Problem with this script is it upload two image:
The renamed image but unresized.
The non renamed image but resized.
Why ?
You are creating a image, then moving the uploaded file, which is why it's creating two files. I believe your code is supposed to be:
$file_name = $_FILES['HOT_Logo']['name'];
$file_tmp_name = $_FILES['HOT_Logo']['tmp_name'];
$file_target = '../../images/hotel-logos/';
$file_size = $_FILES['HOT_Logo']['size'];
// Resize
$ratio = $width/$height;
if($ratio > 1) {
$new_width = 300;
$new_height = 300/$ratio;
}
else {
$new_width = 300*$ratio;
$new_height = 300;
}
// Rename file
$temp = explode('.', $file_name);
$newfilename = 'new_img_name.'.end($temp);
// Upload image
if(move_uploaded_file($file_tmp_name , $file_target.$newfilename)) {
$src = imagecreatefromstring(file_get_contents($file_target.$newfilename));
$dst = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagedestroy($src);
imagepng($dst, $file_target.$newfilename);
imagedestroy($dst);
....
}

resizing photo in php

This is the code provided in an example at: http://www.php.net/manual/en/function.imagecopyresampled.php
test.jpg size: 500x357
phpdotnet_resize.php
This displays test.jpg resized at 200x142
<?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);
echo "<img src='$filename'>";
?>
However, this displays test.jpg in its original size of 500x357
<?php
include "phpdotnet_resize.php";
echo "<img src='$filename'>";
?>
I am trying to move the resized test.jpg to images/ but it keeps moving the original size image.
upload.php (only relevant snippet showing)
include "phpdotnet_resize.php";
if(move_uploaded_file($file_tmp,"images/".$filename)){
echo "Success";

resize image - showing only half if the pic

this script some how showing half of the image not the whole image... i am trying to figure this out what is wrong here
any help please
// The file
$ftimage = $_POST['feature_image'];
// maximum height 150 X width 150
$width = 150;
$height = 150;
// Dimensions Set
list($width_orig, $height_orig) = getimagesize($ftimage);
$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($ftimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
$image = imagejpeg($image_p, null, 100);
var_dump($image);
Try to replace it
$ftimage = $_POST['feature_image'];
with it:
$ftimage = $_POST['feature_image']['tmp_name'];
Or if in your POST request stores a URL of remote image, try something like that:
$image = imagecreatefromstring(file_get_contents($ftimage));

download image from remote source and resize then save

Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?
So basically I would have an image at "http://www.site.com/image.jpg" save to my web server "/images/myChosenName.jpg" as a 120x120 pixels.
Thanks
You can try this:
<?php
$img = file_get_contents('http://www.site.com/image.jpg');
$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = '120';
$newheight = '120';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
?>
More information about PHP image function : http://www.php.net/manual/en/ref.image.php
You can resize keeping the ratio of image
$im = imagecreatefromstring($img);
$width_orig = imagesx($im);
$height_orig = imagesy($im);
$width = '800';
$height = '800';
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
If you're looking to have the ability to do this for both jpg and png file formats, here's what helped me:
$imgUrl = 'http://www.example.com/image.jpg';
// or $imgUrl = 'http://www.example.com/image.png';
$fileInfo = pathinfo($imgUrl);
$img = file_get_contents($imgUrl);
$im = imagecreatefromstring($img);
$originalWidth = imagesx($im);
$originalHeight = imagesy($im);
$resizePercentage = 0.5;
$newWidth = $originalWidth * $resizePercentage;
$newHeight = $originalHeight * $resizePercentage;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
if ($fileInfo['extension'] == 'jpg') {
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg($tmp, '/img/myChosenName.jpg', -1);
}
else if ($fileInfo['extension'] == 'png') {
$background = imagecolorallocate($tmp , 0, 0, 0);
imagecolortransparent($tmp, $background);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagepng($tmp, '/img/myChosenName.png');
}
else {
// This image is neither a jpg or png
}
imagedestroy($tmp);
imagedestroy($im);
The extra code on the png side of things ensures that the saved image contains any and all transparent sections.

Categories