PHP image resize black background - change colour - php

So I am using this code to resize an image on the fly and create a copy of it.
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
But what I don't like is it gives a default black background. How could I change this black background to something like, white?
Here it is:

After you initialize $tci, you can use imagefill() and imagecolorallocate() to fill the image with white:
$white = imagecolorallocate($tci, 255, 255, 255);
imagefill($tci, 0, 0, $white);
Note: this will only work if the source image uses transparent pixels.

Related

PHP Resize png image add black background [duplicate]

I am using this code for resizing:
<?php
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
But everytime I upload a transparant png image it turns out with a black background. How do I prevent this from happening and maintain the transparant background?
try this before imagecopyresampled, added alpha blending, and then save alpa
imagealphablending($tci, false);
imagesavealpha($tci,true);
$transparent = imagecolorallocatealpha($tci, 255, 255, 255, 127);
imagefilledrectangle($tci, 0, 0, $w, $h, $transparent);
Try setting the transparent color using imagecolortransparent:
http://il.php.net/manual/en/function.imagecolortransparent.php
Try this, for making your re-size function
<?php
$file_name =testimg.jpg // your file name
extension= explode(".", strtolower($file_name));//get ext of your image
if($extension[1]=="jpg" || $extension[1]=="jpeg" )
{
$src = imagecreatefromjpeg($filename); // Check ext and set src
}
else if($extension[2]=="png")
{
$src = imagecreatefrompng($filename); // Check ext and set src
}
else
{
$src = imagecreatefromgif($filename); // Check ext and set src
}
list($width,$height)=getimagesize($filename);
$newwidth=round($width/10); //resized image width
$newheight=round($height/10); //resized image height
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$resizedfile = "resizeduploads/". $name;
if($extension[1]=="jpg" || $extension[1]=="jpeg" )
{
imagejpeg($tmp,$resizedfile);
}
else if($extension[1]=="png")
{
imagepng($tmp,$resizedfile);
}
else
{
imagegif($tmp,$resizedfile);
}
imagedestroy($src);
imagedestroy($tmp);

Resize image to equal width and height while uploading in php

I'm trying to automatically resize the image while uploading by using this function:
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
and then :
$target_file = "img/".$admit_roll.".".$ext;
$resized_file = "img/".$admit_roll.".".$ext;
$wmax = 200;
$hmax = 200;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $ext);
But this isn't returning a square sized-image even when $wmax abd $hmax are same. How should make it return a square size image ?
You could use the function imagecopyresized() to resize it:
function img_resize($target, $newcopy, $w, $h, $ext){
list($w_orig, $h_orig) = getimagesize($target);
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
imagecopyresized($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy);
}
With this function you don't have to care about aspect ratio, the function does for you.
Edit: You can replace the function imagecopyresized()with imagecopyresampled(), it will do the same thing, although with more quality, but using more cpu time
Image manipulation is complex. You can learn to do it yourself, that's fun and sometimes useful. But it also tedious as you need to learn all the problems that others already solved.
Another alternative is to use one of the libraries that make it easy for you to achieve this.
For example: Intervention Image
Take a look how its easier to fit image into a frame.
// open file a image resource
$img = Image::make('public/foo.jpg');
// crop the best fitting 5:3 (600x360) ratio and resize to 600x360 pixel
$img->fit(600, 360);
// crop the best fitting 1:1 ratio (200x200) and resize to 200x200 pixel
$img->fit(200);
// add callback functionality to retain maximal original image size
$img->fit(800, 600, function ($constraint) {
$constraint->upsize();
});

PNG with transparent background = thumbnail with black backgroud

Can you support me?
I have the following script in order to create a thumbnail, works fine!
BUT, when I upload a PNG file with transparent background, for some reason the background change to black.
<?php
// Function for resizing jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
If the background of the PNG it's transparent, I need the thumbnail transparent also, can you please support me?
Any help will be GREAT!
Thanks in advance
Try to replace this:
imagejpeg($tci, $newcopy, 80);
by this:
imagegif($tci, $newcopy, 80);
Or the equivalent function depending on the image format:
Imagepng
Imagegif
Imagejpeg
Imagebmp
Using:
$tci = imagecreate($w, $h);
Instead:
$tci = imagecreatetruecolor($w, $h);
My issue is now solved :)

PHP Resize image gives black background

I am using this code for resizing:
<?php
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
But everytime I upload a transparant png image it turns out with a black background. How do I prevent this from happening and maintain the transparant background?
try this before imagecopyresampled, added alpha blending, and then save alpa
imagealphablending($tci, false);
imagesavealpha($tci,true);
$transparent = imagecolorallocatealpha($tci, 255, 255, 255, 127);
imagefilledrectangle($tci, 0, 0, $w, $h, $transparent);
Try setting the transparent color using imagecolortransparent:
http://il.php.net/manual/en/function.imagecolortransparent.php
Try this, for making your re-size function
<?php
$file_name =testimg.jpg // your file name
extension= explode(".", strtolower($file_name));//get ext of your image
if($extension[1]=="jpg" || $extension[1]=="jpeg" )
{
$src = imagecreatefromjpeg($filename); // Check ext and set src
}
else if($extension[2]=="png")
{
$src = imagecreatefrompng($filename); // Check ext and set src
}
else
{
$src = imagecreatefromgif($filename); // Check ext and set src
}
list($width,$height)=getimagesize($filename);
$newwidth=round($width/10); //resized image width
$newheight=round($height/10); //resized image height
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$resizedfile = "resizeduploads/". $name;
if($extension[1]=="jpg" || $extension[1]=="jpeg" )
{
imagejpeg($tmp,$resizedfile);
}
else if($extension[1]=="png")
{
imagepng($tmp,$resizedfile);
}
else
{
imagegif($tmp,$resizedfile);
}
imagedestroy($src);
imagedestroy($tmp);

Resize Image PNG With transparence

I want to resize an image PNG with transparence plz help.
Here is the code :
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
Try this
UPDATED
function createThumb($upfile, $dstfile, $max_width, $max_height){
$size = getimagesize($upfile);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height)) {
$tn_width = $width;
$tn_height = $height;
} elseif (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
if($size['mime'] == "image/jpeg"){
$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imageinterlace( $dst, true);
ImageJpeg($dst, $dstfile, 100);
} else if ($size['mime'] == "image/png"){
$src = ImageCreateFrompng($upfile);
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($src, 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($src, $background);
// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($src, false);
// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($src, true);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
Imagepng($dst, $dstfile);
} else {
$src = ImageCreateFromGif($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imagegif($dst, $dstfile);
}
}
There a simple to use, open source library called PHP Image Magician. It uses GD and supports transparency
Example of basis usage:
$magicianObj = new imageLib('racecar.png');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');

Categories