PHP reduce size and quality with multiple images - php

this function work only with one picture. The function work only if I call the function once.
function imgToThumbnail($name) {
$filename = $name;
$percent = 0.05;
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$thumbnail=imagejpeg($image_p, null, 90);
imagedestroy($image_p);
return $thumbnail;
}
imgToThumbnail("01.jpg"); // ok
imgToThumbnail("02.jpg"); // not working
I forgot something?

Related

Not true color with imagecreatetruecolor & imagecreatefromjpeg

Hy there,
I use this code to resize my images to improve size...
<?php
// File
$filename = 'ok.jpg';
list($width, $height) = getimagesize($filename);
$ratio = ($width >= $height) ? 683 : 1152;
$percent = $ratio / $height;
// New sizes
$new_width = $width * $percent;
$new_height = $height * $percent;
// Apply new sizes
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Save
imagejpeg($image_p, 'tes.jpg', 80);
imagedestroy($image);
imagedestroy($image_p);
But I saw a problem with the colors quality... image looks more grey than original.
Original picture :
And after resizing :
What can I do to keep same colors ?
Thanks

PHP Image Resize Fatal error: Out of memory

I have following PHP code that resizes my pictures to desired size:
/* POSTER - resize */
$remote_file = $castImages[$name];
$new_width = 296;
$new_height = 436;
list($width, $height) = getimagesize($remote_file);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($remote_file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, '../glumci/'.$name.' BIG.jpg', 100);
$new_width = 74;
$new_height = 109;
list($width, $height) = getimagesize($remote_file);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($remote_file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, '../glumci/'.$name.'.jpg', 100);
imagedestroy($image_p);
imagedestroy($image);
I have around 5500 pictures to be resizes, so i run this code into the while loop and get this error from PHP:
Fatal error: Out of memory (allocated 473956352) (tried to allocate 27263000 bytes) in D:\portal_ONLINE\UwAmp\www\inc\test.php on line 54
I then add in PHP script this code:
ini_set('memory_limit', '-1');
But i receive same error message..so how to fix this error so that script rename all 5500pictures not just 50 and throw this error?
After help from members from above replay answers i got finnaly working code:
/* POSTER - resize */
$remote_file = $castImages[$name];
$new_width = 296;
$new_height = 436;
list($width, $height) = getimagesize($remote_file);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($remote_file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, '../glumci/'.$name.' BIG.jpg', 100);
$image_p = null;
$image = null;
$new_width = 74;
$new_height = 109;
list($width, $height) = getimagesize($remote_file);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($remote_file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, '../glumci/'.$name.'.jpg', 100);
imagedestroy($image_p);
imagedestroy($image);
$image_p = null;
$image = null;
Using:
$image_p = null;
$image = null;
It works now about 20minutes and script is working and not trowing error message.

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

Adding logo as watermark, watermark poor quality

I am adding a transparant logo as watermark over an image using PHP. However, in the result the logo has poor quality (the image that is under it is high quality, so it's just the watermark). This is the code I use (its about the last 3 lines):
header("Content-Type: image/png");
$photo = imagecreatefromjpeg('photos/'.$photo['image']);
$height = imagesx($photo);
$width = imagesx($photo);
if ($width > $_POST['width']) {
$r = $width / $_POST['width'];
$newwidth = $width / $r;
$newheight = $height / $r;
}
$image = imagecreatetruecolor($width, $height);
$image2 = imagecopyresampled($image, $photo, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$position = explode(" ", $_POST['background']);
$image3 = imagecrop($image, [
'x' => str_replace(array('-', 'px'), array('', ''), $position[0]),
'y' => str_replace(array('-', 'px'), array('', ''), $position[1]),
'width' => $_POST['width'],
'height' => $_POST['height']
]);
$stamp = imagecreatefrompng('img/logo.png');
imagecopyresized($image3, $stamp, 0, 0, 0, 0, 147, 50, imagesx($stamp), imagesy($stamp));
imagepng($image3, "created/".time().".png", 9);
imagecopyresized will copy and scale and image. This uses a fairly primitive algorithm that tends to yield more pixelated results.
a simple example for a better quality is:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// 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);
?>
you should have a look at this post here
use quality of image from 1-100.
imagejpeg($image, $new_image_name, 99);

php read and write an image in url

I would like to resize an image in URL such as localhost/changimage.php?percent=40. I have already resized it in php. But user should resize it by typing in URL
here is my PHP code:changimage.php
<?php
$filename = '/var/www/html/zahir/images/bike.jpeg';
$percent = 0.5;
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename)
;
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, 100);
?>
I have no idea how to do it in URL.
OR
$percent = 0.5;
if (isset($_GET['percent'])) {$percent = $_GET['percent']/100;}
To check if percent has a value
Try it.
$percent = 0;
if (isset($_GET["percent"])) {
$percent = ($_GET["percent"] / 100);
}
if ($percent <= 0) {
$percent = 1;
}
$filename = '/home/deepakgupta/Pictures/phone.jpg';
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename)
;
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, 100);
maybe something like that:
$percent = 0.5;
if ($_GET['percent']) {$percent = $_GET['percent']/100;}

Categories