Unable to move resized images to S3 - php

I want to upload my image in amazon s3 bucket .My original image upload successfully but thumbnail not uploaded. i referred some solution from stackflow .. nothing meet solution for this.. am uploading multiple images.. original images can upload successfully..
Here is my code i have tried
$fileCount = count($_FILES["image"]['name']);
//echo $fileCount;
for($i=0; $i < $fileCount; $i++)
{
$ImageName = str_replace(' ','-',strtolower($_FILES['image']['name'][$i]));
$tmp = $_FILES["image"]["tmp_name"][$i];
$actual_image_name = "slider".time().$ImageName;
$new = "slider_new".time().$ImageName;
//echo $actual_image_name."<br>";
$data=array('slider_image'=>$NewImageName);
if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ))
{
$targetFile ='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
$image = new SimpleImage();
$image->load($targetFile);
$fileTempName=$image->resize(80,80);
$image->save('http://'.$bucket.'.s3.amazonaws.com/');
if($s3->putObjectFile($fileTempName, $bucket , $new, S3::ACL_PUBLIC_READ))
{
//insert query to database
}
}
}
Here is the class of resizing image
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
// echo"jpg";
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
//echo"png";
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
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;
echo $this->image;
}
}

Related

Yii2 imagecreatefrompng do not work with transparent

I use custom model to crop and commpress my image file when uploading. The problem is when upload image/png ... it crop and compress the image, but transparent background replace with black background ... i can not see my mistake ...
here is upload function in controller
$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles[' . $imgKey . ']'); // get the imageFiles
$pic = Yii::getAlias('#frontend/web') . '/product_photos/thumb-270/' . $model->getImageFolderName() . '/' . $fileName; // set the thumb path
$pic2 = Yii::getAlias('#frontend/web') . '/product_photos/' . $model->getImageFolderName() . '/' . $fileName; // set reale image path
$file->saveAs(Yii::getAlias('#frontend/web') . '/product_photos/' . $model->getImageFolderName() . '/' . $fileName);
$image = file_get_contents(Yii::getAlias('#frontend/web') . '/product_photos/' . $model->getImageFolderName() . '/' . $fileName);
file_put_contents($pic, $image);
$model->resizeImg($pic);
if($file->type!='image/png') {
$settings->compress($pic, $pic, 90);
$settings->compress($pic2, $pic2, 90);
}
here is a model function resizeimg:
public function resizeImg($img) {
$sz = getimagesize($img);
$ratio = $sz[0] / $sz[1]; // w/h
$w2 = Yii::$app->params['thumbswidth']; // thumb 1 width
$image = new SimpleImage();
$image->load($img);
$image->resize($w2, round($w2 / $ratio));
$image->save($img);
}
and here is all model SimpleImage:
var $image;
var $image_type;
function load($DATA, $FORMNAME = NULL) {
$image_info = getimagesize($DATA);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($DATA);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($DATA);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($DATA);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
imagesavealpha($new_image, true);
$this->image = $new_image;
}

image resizing png adding black colour in background

I am resizing images using the code below, but their is problem with resizing png images with transparent background that it fills the background with black color.
Below is the full code of image resizing:
function store_uploaded_image($html_element_name, $new_img_width, $new_img_height ,$size ,$target_dir) {
$target_file = $target_dir .'image-'.$size.'.png';
$image = new SimpleImage();
$image->load($_FILES[$html_element_name]['tmp_name']);
$image->resize($new_img_width, $new_img_height);
$image->save($target_file);
return $target_file; //return name of saved file in case you want to store it in you database or show confirmation message to user
}
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_PNG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
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;
}
}
EDIT:
as #Marc B suggested, using imagealphablending and imagesavealpha before imagecopyresampled made it to work.
imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );

Function to resize jpg file compresses it too

I have a script which resizes images on server side before loading it to the client side.
Issue with this script is it compresses the file too, and that ends up in bad quality images.
Have a look at the script:
Image_Resize.php
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=0, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,100);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if ($this->image_type == IMAGETYPE_PNG){
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
}
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
?>
Photo.php
<?php
require 'Image_Resize.php';
header('Content-Type: image/jpeg');
$imagenew=$_GET['id'];
$name=substr($imagenew,1);
if(file_exists($name)){
$image = new ResizeImage();
$image->load($imagenew);
$image->resize($_GET['width'], $_GET['height']); //25-width, 30-height
$image->output();
}
?>
I tried passing quality parameter to function imagejpeg in output function in Image_resize.php file. It's like this: imagejpeg($this->image,$filename,100);
But it's not working either(still outputs poor result than the original image). Anybody help?

PHP Resize Images and Save to Server [duplicate]

This question already has answers here:
php - resize and save an image? [duplicate]
(3 answers)
Closed 9 years ago.
I'm resizing and saving multiple images from a URL and was wondering how I can compress these images more as the images that are saving in the 640x320 folder are 400kb which is too big and was wondering how I can compress these images more, thanks in advance for any advice!
PHP RESIZE AND SIZE HANDLER
include("../includes/picture-resize.php");
$image = $_POST['thumbnail'];
$slug = $_POST['slug'];
$images = $_POST['screenshots'];
$list = explode(",", $images);
$listlength = count($list);
$i = 0;
$image = $_POST['thumbnail'];
$path = parse_url($image, PHP_URL_PATH);
$filename = $slug.'-'.$i;
$extension = pathinfo($path, PATHINFO_EXTENSION);
$file = $filename.'.'.$extension;
file_put_contents('../tmp/' . $file, file_get_contents($image));
$picture = new pic_resize();
$picture->load('../tmp/'.$file);
$picture->resizeToWidth(125);
mkdir('../images/125x125/'.$slug);
$picture->save('../images/125x125/'.$slug.'/'.$file, $picture->image_type);
unlink('../tmp/'.$file);
$thumbnail = $file;
$new_list = array();
mkdir('../images/640x320/'.$slug);
mkdir('../images/310x205/'.$slug);
while($listlength > $i) {
$path = parse_url($list[$i], PHP_URL_PATH);
$filename = $slug.'-'.$i;
$extension = pathinfo($path, PATHINFO_EXTENSION);
$file = $filename.'.'.$extension;
file_put_contents('../tmp/' . $file, file_get_contents($list[$i]));
$picture = new pic_resize();
$picture->load('../tmp/'.$file);
$picture->resizeToWidth(640);
$picture->save('../images/640x320/'.$slug.'/'.$file, $picture->image_type);
$picture->resizeToWidth(310);
$picture->save('../images/310x205/'.$slug.'/'.$file, $picture->image_type);
unlink('../tmp/'.$file);
array_push($new_list, $file);
$i++;
}
PHP RESIZE CLASS
class pic_resize{
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename,9,PNG_FILTER_PAETH);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
3rd parameter:
JPEG Quality : Compression level: from 0 (most compression) to 100 (least compression).
http://www.php.net/manual/en/function.imagejpeg.php
PNG Quality : Compression level: from 0 (no compression) to 9.
http://www.php.net/manual/en/function.imagepng.php
Note that that the quality/size settings are pretty much different/backwards between jpeg and png

Dynamically resample hotlinked images using php

I have an images directory, some with massive resolutions, so I'd like to serve a much lower resolution version if they get hotlinked and I'm also overlaying a repeating image (like a watermark) informing the viewer that the image is stolen and where they can find the genuine original.
I've got the resampling working fine but when I add the function for the no hotlink image overlay, it doesn't work. The thing is, I know the watermarking script works too, on it's own, because I've used it elsewhere in another file on the site. Unfortunately I can't tell what the error is as the hotlink test sites don't output errors, they just show an empty image placeholder, and my host's log files are all like greek to me.
This is the script I have at the moment:
<?php
ini_set('memory_limit','250M');
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=60, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
imagedestroy($image);
exit();
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
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;
}
function nothot() {
$hotlink = imagecreatefrompng('hotlink.png');
$hw = imagesx($hotlink);
$hh = imagesy($hotlink);
$img_paste_x = 0;
$img_paste_x = 0;
while($img_paste_x < $this->getWidth()){
$img_paste_y = 0;
while($img_paste_y < $this->getHeight()){
imagecopy($image, $hotlink, $img_paste_x, $img_paste_y, 0, 0, $hw, $hh);
$img_paste_y += $hh;
}
$img_paste_x += $hw;
}
imagedestroy($hotlink);
}
}
header('Content-Type: image/jpeg');
$image = new SimpleImage();
$image->load($path);
$image->resizeToWidth(600);
$image->nothot();
$image->output();
?>
Thanks for your help.
Change:
imagecopy($image, $hotlink, $img_paste_x, $img_paste_y, 0, 0, $hw, $hh);
to:
imagecopy($this->image, $hotlink, $img_paste_x, $img_paste_y, 0, 0, $hw, $hh);
Also not sure about the way you get the $path, some checks on whether the image exists wouldn't go a miss.

Categories