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
Related
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.
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');
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:
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?
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;}