when I trying to resize an PNG
with
function resize($width,$height){
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type == IMAGETYPE_PNG || $this->type == 'image/png')
{
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;
}
function output($image_type=IMAGETYPE_JPEG, $compression=100){
if($image_type == IMAGETYPE_JPEG){
imagejpeg($this->image, null, $compression);
}elseif($image_type == IMAGETYPE_GIF){
imagegif($this->image);
}elseif($image_type == IMAGETYPE_PNG){
imagepng($this->image);
}
}
then the background of the resized image is black, I have tried some differnt solutions bus nohting worked yet.
Related
My webapp is ajaxing an image on server. If the image size is lower than a defined value the script works as expected. If greater, the image is resized with GD. It works fine with PNG and GIF files but returns an Error 500 with JPG images.... Where am I wrong ?
if (strtolower($e=='.jpg') || strtolower($e=='.jpeg')) {
header('Content-type; image/jpeg');
$gd = imagecreatetruecolor($w, $h);
$image = imagecreatefromjpeg($img);
imagecopyresampled($gd, $image, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagejpeg($gd,$img_temp,100);
}
if (strtolower($e=='.png')) {
header('Content-Type: image/png');
$gd = imagecreatetruecolor($w, $h);
$image = imagecreatefrompng($img);
imagecopyresampled($gd, $image, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagepng($gd,$img_temp,9);
}
if (strtolower($e=='.gif')) {
header('Content-type; image/gif');
$gd = imagecreatetruecolor($w, $h);
$image = imagecreatefromgif($img);
imagecopyresampled($gd, $image, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagegif($gd,$img_temp);
}
I Merged two images. First Image will be always white, extension is PNG and size is 1200px X 628px. Size of Second image is 1000px X 495px. But when i merge that images then white image is converted into black image. and show the background of second image are black.
Please help me how to solve this problem and change the black image into white image.
Add below lines to your code, your issue should be resolved,
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
Just for your reference, full source code for resizing an image,
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if ($this->image_type == IMAGETYPE_PNG){
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;
}
I have this public function:
public function resize($width, $height)
{
$newImage = imagecreatetruecolor($width, $height);
if($this->_imageType == IMAGETYPE_PNG && $this->_transparent === true){
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
imagefilledrectangle($newImage, 0, 0, $width, $height, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
}
imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->_image = $newImage;
}
My problem appears when I preview the image. It doesn't display alpha color. Can I get some help?
Try setting imagealphablending to true, not false.
I'm in need of a generic image upload for a PHP site. Photos and Logos should be re-sized to a certain extend to make sure they're not too big and fit the design.
I'm trying it with this code:
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type == PNG or $this->image_type == GIF) {
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $nWidth, $nHeight, $transparent);
}
imagecopyresized($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
However, when I upload an image that has areas with alpha-values between 0 and 255, they get replaced by a complete black, turning anti-aliased areas to a black border.
The full transparency works fine for PNG and GIF, merely the half-transparent areas are a problem.
I apologise if I'm not using the correct terms to explain my problem, maybe that's why I hardly found anything on it.
Try:
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type == PNG or $this->image_type == GIF) {
imagefill($new_image, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($new_image,true);
imagealphablending($new_image, true);
}
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
Based on this (which I know it works).
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');