<?
/*
* Class: SimpleImage, Author: Simon Jarvis, Copyright: 2006 Simon Jarvis, Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
* See the GNU General Public License for more details: http://www.gnu.org/licenses/gpl.html
*/
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=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;
}
}
// End of SimpleImage class
/* Directory Listing
Source : http://www.spoono.com/php/tutorials/tutorial.php?id=10
*/
// Define the path as relative
$path = "/home/content/51/6511651/html/SITE/img2";
// Using the opendir function
$dir_handle = #opendir($path) or die("ERROR: Cannot open <b>$path</b>");
echo("Directory Listing of $path<br/>");
//running the while loop
while ($file = readdir($dir_handle))
{
if($file != "." && $file != "..")
{
$image = new SimpleImage();
$image->load("/img2/new".$file);
$image->resize(86,86);
$image->save("img2/new/s".$file);
echo("• $file <br>");
}
}
//closing the directory
closedir($dir_handle);
?>
Why won't this script do anything???? I was trying to get it configured from (http://www.webmastersucks.com/resizing-all-images-in-directory/) and it basically does nothing once i run it.
Anyone?
Firstly, after this line:
$path = "/home/content/51/6511651/html/SITE/img2";
Add this one:
if (getcwd() != $path) chdir($path);
...to make sure you are working in a directory that allows you to use the relative paths you are trying to.
Then, change the contents of the while loop to this:
if($file != "." && $file != "..") {
$image = new SimpleImage();
$image->load($file);
$image->resize(86,86);
$image->save("s".$file);
echo("• $file <br>");
}
Related
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;
}
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;
}
}
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 );
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
I am using image upload and resize script from someones blog. It stores the name of file as resize.jpg .however i want to give unique names to them as data will go in database...I am very bad in using functions so please guide me.
<?php
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=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;
}
}
?>
<?php
if( isset($_POST['submit']) ) {
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($_FILES['uploaded_image']['tmp_name']);
$image->resizeToWidth(300);
$image->resizeToHeight(200);
$image->save('images/resize.jpg');
//$image->output();
} else {
?> <form action="" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_image" />
<input type="submit" name="submit" value="Upload" />
</form><?php
}
?>
Inside this block, you are saving every one as resize.jpg
$image->resizeToWidth(300);
$image->resizeToHeight(200);
$image->save('images/resize.jpg');
You can generate a unique name with uniqid() and optionally add a prefix onto it like img_. Your filenames will then look like:
'img_4e297753130db.jpg'
Start by generating a filename, saved in a variable.
$prefix = "img_"
$new_filename = uniqid($prefix) . ".jpg";
// Do your other processing
// ...
$image->resizeToWidth(300);
$image->resizeToHeight(200);
// Save with the new filename
// Note change to double quotes from single...
$image->save("images/$new_filename");
Later, when you're ready to store the filename in the database, it's still available in $new_filename
The image name is set in this line:
$image->save('images/resize.jpg');
You will need to determine how you want to name the output files, and modify that line to produce the desired output.
If you can provide more specific detail regarding your statement "i want to give unique names," please update your question.