Some questions about saving images via PHP - 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.

Related

why image background color is showing me blurry when I crop the image using PHP?

I am using following php function to crop a image. When a image is cropped then the image background is look like a blurry. Like bellow image. It's should be solid white background color :
Blurry Image :
Php code is bellow :
function crop_image ($target, $newcopy, $w, $h, $ext) {
$ext = strtolower($ext);
if ($ext == "gif"){
$image = imagecreatefromgif($target);
} else if($ext =="png"){
$image = imagecreatefrompng($target);
} else {
$image = imagecreatefromjpeg($target);
}
$filename = $newcopy;
$thumb_width = $w;
$thumb_height = $h;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
$color = imagecolorallocate($thumb, 255, 255, 255);
imagefill($thumb, 0, 0, $color);
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}
You're feeding a $quality value of 80 into imagejpeg. JPEG is a lossy format. This results in artifacts like the ones you're seeing:
quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75).
Try using a higher value:
imagejpeg($thumb, $filename, 90);
The maximum value for $quality is 100.

Crop and resize images in php

I am trying to crop and resize the images. When images moved to the resized_images folder all images are turn to black but images are resized(535 * 313). Here is my code i have tried so far. Can you please suggest me right way to do this.? thank u
<form action="" method="POST" enctype="multipart/form-data">
<input id="input-6" name="slideshow_images[]" type="file" multiple class="file-loading">
<input type="submit" name="sub" >
</form>
<?php
if(isset($_POST['sub']))
{
$pic = $_FILES["slideshow_images"]["name"];
foreach($pic as $pic_src)
{
$image = imagecreatefromjpeg($pic_src);
$filename = 'resized_images/'.$pic_src.'cropped_whatever.jpeg';
$thumb_width = 535;
$thumb_height = 313;
$width = imagesx($image);
$height = imagesy($image);
$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 = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}
}
?>
Change your code lines to:
$pic = $_FILES["slideshow_images"]["tmp_name"];
$image = imagecreatefromstring(file_get_contents(($pic_src)));
Because ["name"] is only 123.jpg, it is not object.
The best way will be that:
<form action="" method="POST" enctype="multipart/form-data">
<input id="input-6" name="slideshow_images[]" type="file" multiple class="file-loading">
<input type="submit" name="sub" >
</form>
<?php
if(isset($_POST['sub'])){
if(isset($_FILES['slideshow_images'])){
foreach ($_FILES["slideshow_images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["slideshow_images"]["tmp_name"][$key];
$name = $_FILES["slideshow_images"]["name"][$key];
$image = imagecreatefromstring(file_get_contents(($tmp_name)));
$filename = 'images/'.$name.'cropped_whatever.jpg';
$thumb_width = 535;
$thumb_height = 313;
$width = imagesx($image);
$height = imagesy($image);
$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 = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}
}
}
}
?>
Also, If you don't want to add ".jpg" in filename, replace $filename row to this:
$filename = 'images/'.preg_replace('/\.[^.]*$/', '', $name).'cropped_whatever.jpg';
I just ran into this problem. What the problem is is the background color is black and totally transparent. What you have to do is just allocate a true color (like white) with alpha and make the alpha totally non-transparent. Then just do a filled rectangle over the new area first and your image should then show up. :-)
The following is directly out of the php documentation about imagecopy:
// create new image with padding
$img = imagecreatetruecolor($right-$left+$padding*2,$bottom-$top+$padding*2);
// Allocate background color
$white = imagecolorallocatealpha( $img, 255, 255, 255, 0 );
// fill the background
imagefill($img, 0, 0, $white);
// or use
imagefilledrectangle( $img, 0,0,$width,$height, $white );
// copy
imagecopy($img, $image, $padding, $padding, $left, $top, $right-$left, $bottom-$top);
Notice that they do an imagefill with the background color before actually copying the new image. It is the same for the imagecopyresample.
Well, unlike before - this time I didn't get a black image. So check what you do against the following: In fact, download the following and run it (along with the test.jpg image). See if it works for you. Please note that this is directly out of the PHP Documentation website for imagecopyresample.
<?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, "new.jpg", 100);
?>
And here is the image:
And here is the output:

Cropping any image down to 4:3 ratio

I'm trying to modify an upload script, at the moment I can crop an image into a square while resizing - great!
However I would like the user to be able to upload any size image, and for the script to create 200x150, 400x300, 800x600 thumbnail/images - a 4:3 ratio.
My code so far is:
list($width,$height) = getimagesize($uploadedfile);
if ($thumb == 1){
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
// copying the part into thumbnail
$thumbSize = 200;
$tmp = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($tmp, $src, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
// write thumbnail to disk
$write_thumbimage = $folder .'/thumb-'. $image;
switch($ext){
case "gif":
imagegif($tmp,$write_thumbimage);
break;
case "jpg":
imagejpeg($tmp,$write_thumbimage,100);
break;
case "jpeg":
imagejpeg($tmp,$write_thumbimage,100);
break;
case "png":
imagepng($tmp,$write_thumbimage);
break;
}
Does anyone know the required formula or can point me in the right direction?
Worked out the logic using some of what was on the C# duplicate:
$thumb_width = 200;
$thumb_height = 150;
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect ) {
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
} else {
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$tmp = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($tmp,
$src,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);

PHP Crop png is not working

not work?
$image = imagecreatefrompng("$filename");
My php code for crop photo
$filename="yaaa.png";
$width: 10px;
$height: 10px;
$image = imagecreatefrompng("$filename");
$thumb_width = $width;
$thumb_height = $height;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2,
0 - ($new_height - $thumb_height) / 2,
0, 0,
$new_width, $new_height,
$width, $height);
imagepng($thumb, $filename, 100);
imagedestroy($thumb);
im run this code but not working, my pict is dark and text "picture not valid"
why?
The definition of $width and $height is invalid. Change it like this:
$width = 10;
$height = 10;

PHP imagecopyresampled() without height

I have this piece of code here
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
Baiscally what I am trying to do is upload an image and resize the width and have the height adjusted based on the width.
I tried this also
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width);
without the height and got this error
Warning: Wrong parameter count for imagecopyresampled() in /home/content/44/8713044/html/admin/Categories.php on line 63
this is the current code where the $width and $height variables come from.
if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
Any Help would be appreciated, Thanks in advanced,
J
Here is the full function..
function create_thumbnail($source,$destination, $thumb_width) {
$percent = 0.5;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
$x = 0;
$y = 0;
if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
$new_image = imagecreatetruecolor($thumb_width,$thumb_width)or die('Cannot Initialize new GD image stream');
$extension = get_image_extension($source);
if($extension=='jpg' || $extension=='jpeg')
$image = imagecreatefromjpeg($source);
if($extension=='gif')
$image = imagecreatefromgif($source);
if($extension=='png')
$image = imagecreatefrompng($source);
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
if($extension=='jpg' || $extension=='jpeg')
imagejpeg($new_image,$destination);
if($extension=='gif')
imagegif($new_image,$destination);
if($extension=='png')
imagepng($new_image,$destination);
}
the $thumb_width is 600 and this returns my image 600*600
This worked perfectly when i tested it .... Except you want to work with fixed size
$filename = "a.jpg" ;
$percent = 0.5;
header('Content-Type: image/jpeg');
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);
imagejpeg($image_p, null, 100);
Demo

Categories