How to write an image after after having resize it - php

I am trying to resize photos with a function that I get from the official website of PHP. It's a function that resize photos without losing their ratio.
public function ImageResize($filename, $max_width,$max_height){
list($orig_width,$orig_height) = getimagesize($filename);
$width = $orig_width;
$height = $orig_height;
#c'est la photo est grande.
if($height > $max_height){
$width = ($max_height/$height) * $width;
$height = $max_height;
}
#c'est la photo est larage
if($width > $max_width){
$height = ($max_width/$width) * $height;
$width = $max_width;
}
$image_p = imagecreatetruecolor($width,$height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0,
$width, $height, $orig_width, $orig_height);
return $image_p;
}
This function is supposed to send me a picture, the question is: does the reduced image is automatically written to disk or I have to do more processing to make the change between the old and the new picture.

The image will only be available in the variable $image_p until you save it. Like this:
imagejpeg($image_p, 'your_image_in_disk.jpg');

Related

PHP - Create thumbnail from picture and keeping proportion

I want to create a thumbnail image without black/white bars and have it keep aspect ratio
The thumbnail size should be 320x200 (px).
I actually wrote a function to create a thumbnail for a given resolution but I don't know how to keep the aspect ratio of the image
function imageResize($imageResourceId, $width, $height)
{
$targetWidth = 320;
$targetHeight = 200;
$targetLayer = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetLayer, $imageResourceId, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
return $targetLayer;
}
But I can't figure out a way to crop them and have them accommodated as I want. Thanks in advance!
To do this you can use imagecopyresampled function like this:
function imageResize($imageResourceId, $width, $height)
{
$targetWidth = 320;
$targetHeight = 200;
$aspectRatio = $width / $height;
$targetRatio = $targetWidth / $targetHeight;
if ($aspectRatio > $targetRatio) {
$newHeight = $targetHeight;
$newWidth = $targetHeight * $aspectRatio;
} else {
$newWidth = $targetWidth;
$newHeight = $targetWidth / $aspectRatio;
}
$targetLayer = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($targetLayer, $imageResourceId, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
return $targetLayer;
}
Using this, the new Width and Height are calculated based on the aspect ratio of the original image.
More samples on: https://www.php.net/manual/en/function.imagecopyresampled.php

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 GD cropping PNG doesn't work

I'm creating a function to crop images using PHP GD.
The function works perfectly for JPEG images but when I try to use the same function for PNG I just get a blank page and the following errors when I inspect the page in chrome:
I realize this is a javascript error, but this is probably something chrome does? This is what happens when i visit the IMAGE URL. If i replicate this with the JPEG code it works perfectly.
Edit: in Firefox it says "The image [URL] cannot be displayed because it contains errors. The below js error is just a chrome error and isn't very relevant.
Uncaught TypeError: Cannot read property 'getAttribute' of null
data_loader.js:2 Uncaught TypeError: Cannot read property
'hasAttribute' of null global-shortcut.js:9
The working JPEG code is as follows:
$fileName = $_POST['file'];
$jpeg_quality = 100;
$ratio = $_POST['r'];
$src = '/var/www/admin/public_html/images/'.$fileName;
$image = imagecreatefromjpeg($src);
$target = '/var/www/admin/public_html/images/crop/'.$fileName;
$thumb_width = $_POST['w'] / $ratio;
$thumb_height = $_POST['h'] / $ratio;
$width = imagesx($image);
$height = imagesy($image);
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0,
0,
$_POST['x'] / $ratio, $_POST['y'] / $ratio,
$width, $height,
$width, $height);
imagejpeg($thumb, $target, $jpeg_quality);
The Broken PNG code is as follows:
$fileName = $_POST['file'];
$jpeg_quality = 100;
$ratio = $_POST['r'];
$src = '/var/www/admin/public_html/images/'.$fileName;
$image = imagecreatefrompng($src);
$target = '/var/www/admin/public_html/images/crop/'.$fileName;
$thumb_width = $_POST['w'] / $ratio;
$thumb_height = $_POST['h'] / $ratio;
$width = imagesx($image);
$height = imagesy($image);
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0,
0,
$_POST['x'] / $ratio, $_POST['y'] / $ratio,
$width, $height,
$width, $height);
imagepng($thumb, $target, $jpeg_quality);
I answered my own question.
imagepng() function takes quality argument from 0 to 9
imagejpeg() function takes quality argument from 0 to 100

Some questions about saving images via PHP

I want to save images via PHP.
My code is like this:
$image = imagecreatefrompng("myimage.png");
$width = imagesx($image);
$height = imagesy($image);
if ($width == 0) {
$thumb_width = 0;
$thumb_height = 0;
} else {
$thumb_width = 600;
$thumb_height = (int)(600 * $height / $width);
}
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = (int)($width / ($height / $thumb_height));
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $thumb_height;
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0, 0,
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, "newimage.jpg", 80);
There is a problem, that the newimage.jpg is much darker of the png - Why, and what shall I do in order to save it properly.
Is there any way saving the newimage.jpg with a new opacity? - How can I do that?
Thanks :)
The version of GD that your version of PHP is using doesn't support color profiles.
It's a real issue if you can't update the server, and is annoyingly common.

Creat temporay thumb image in PHP

Hey, i am looking for a method to creat temporay thumb file in PHP. Is there any way not to store the image on the server or delete them right after. What I am looking for is a solution like this: http://www.dig2go.com/index.php?shopbilde=772&type=1&size=120
Could some one explain to me how the php code behind this works? For the moment are am using a php code I found on the net for creation of thumb nails:
function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath)
{
if(!file_exists($pathToImage))
return false;
else
{
//Load image and size
$img = imagecreatefromjpeg($pathToImage);
$width = imagesx($img);
$height = imagesy($img);
//Calculate the size of thumb
$new_width = $thumbWidth;
$new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));
//Make the new thumb
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//Copy the old image and calculate the size
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//Save the thumb to jpg
imagejpeg($tmp_img, $saveNameAndPath);
return true;
}
}
function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath)
{
if(!file_exists($pathToImage))
return false;
else
{
//Load image and size
$img = imagecreatefromjpeg($pathToImage);
$width = imagesx($img);
$height = imagesy($img);
//Calculate the size of thumb
$new_width = $thumbWidth;
$new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));
//Make the new thumb
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//Copy the old image and calculate the size
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// sets the header and creates the temporary thumbnail
// ideal for ajax requests / <img> elements
header("Content-type: image/jpeg");
imagejpeg($img);
return true;
}
}
You can use ini_set("memory_limit","12M"); to set memory limit in your script. You can extend it from 12 megabytes to maximum memory you have.
Generally 64M is good.

Categories