I have an image with size 88x31 and would like to make it 100x100 without resizing the actual image but only its canvas/frame, maybe by copying it in the center of the new blank white image with 100x100 size. Any ideas how to do it?
The correct method is to create a new image and then copy the old image into the middle of it (assuming the starting image is is a JPEG and smaller than 100x100):
$oldimage = imagecreatefromjpeg($filename);
$oldw = imagesx($oldimage);
$oldh = imagesy($oldimage);
$newimage = imagecreatetruecolor(100, 100); // Creates a black image
// Fill it with white (optional)
$white = imagecolorallocate($newimage, 255, 255, 255);
imagefill($newimage, 0, 0, $white);
imagecopy($newimage, $oldimage, (100-$oldw)/2, (100-$oldh)/2, 0, 0, $oldw, $oldh);
You can see here: Thumbnail generation with PHP tutorial
Related
Currently I would like to create a transparent png with the lowest quality .
The code:
<?php
function createImg ($src, $dst, $width, $height, $quality) {
$newImage = imagecreatetruecolor($width,$height);
$source = imagecreatefrompng($src); //imagecreatefrompng() returns an image identifier representing the image obtained from the given filename.
imagecopyresampled($newImage,$source,0,0,0,0,$width,$height,$width,$height);
imagepng($newImage,$dst,$quality); //imagepng() creates a PNG file from the given image.
return $dst;
}
createImg ('test.png','test.png','1920','1080','1');
?>
However, there are some problems:
Do I need to specific a png file before creating any new file? Or can I create without any existing png file?
Warning: imagecreatefrompng(test.png): failed to open stream: No such file or directory in
C:\DSPadmin\DEV\ajax_optipng1.5\create.php on line 4
Although there are error message , it still generate a png file , however, what I found that is the file is a black color image , do I need to specific any parameter to make it transparent?
Thanks.
To 1)
imagecreatefrompng('test.png') tries to open the file test.png which then can be edited with GD functions.
To 2)
To enable saving of the alpha channel imagesavealpha($img, true); is used.
The following code creates a 200x200px sized transparent image by enabling alpha saving and filling it with transparency.
<?php
$img = imagecreatetruecolor(200, 200);
imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);
imagepng($img, 'test.png');
Take a look at:
imagecolorallocatealpha
imagefill
An example function copies transparent PNG files:
<?php
function copyTransparent($src, $output)
{
$dimensions = getimagesize($src);
$x = $dimensions[0];
$y = $dimensions[1];
$im = imagecreatetruecolor($x,$y);
$src_ = imagecreatefrompng($src);
// Prepare alpha channel for transparent background
$alpha_channel = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagecolortransparent($im, $alpha_channel);
// Fill image
imagefill($im, 0, 0, $alpha_channel);
// Copy from other
imagecopy($im,$src_, 0, 0, 0, 0, $x, $y);
// Save transparency
imagesavealpha($im,true);
// Save PNG
imagepng($im,$output,9);
imagedestroy($im);
}
$png = 'test.png';
copyTransparent($png,"png.png");
?>
1) You can create a new png file without any existing one.
2) You get a black color image because you use imagecreatetruecolor();. It creates a highest quality image with a black background. As you need a lowest quality image use imagecreate();
<?php
$tt_image = imagecreate( 100, 50 ); /* width, height */
$background = imagecolorallocatealpha( $tt_image, 0, 0, 255, 127 ); /* In RGB colors- (Red, Green, Blue, Transparency ) */
header( "Content-type: image/png" );
imagepng( $tt_image );
imagecolordeallocate( $background );
imagedestroy( $tt_image );
?>
You can read more in this article: How to Create an Image Using PHP
Trying to put watermark to an image
Image Original Size :- 10.8 MB
After watermarking:- New Image Size:- 37.9 MB
$text = "Afrophoto";
//the text i want as watermark
//create a new image
$newImg = imagecreatefromjpeg($image);
//set the watermark font color to red
$fontColor = imagecolorallocate($newImg, 255, 255, 255);
$font_file = 'fontArial.ttf';
list($width, $height) = getimagesize($image);
//write the watermark on the created image
imagefttext($newImg, 50, 50, $j, $i, $width - 100, $height - 100, $text);
//output the new image with a watermark to a file
imagejpeg($newImg,"uploads/".$_FILES[$field]['name'],100);
imagepng($newImg,"uploads/".$_FILES[$field]['name'].".png");
imagedestroy($newImg);
Here I used Png image
Actual size banned.png: 53.2KB
with watermark bbimage_3.png: 40.8 KB
<?php
$imageURL = "banned.png";
list($width,$height) = getimagesize($imageURL);
$imageProperties = imagecreatetruecolor($width, $height);
$targetLayer = imagecreatefrompng($imageURL);
imagecopyresampled($imageProperties, $targetLayer, 0, 0, 0, 0, $width, $height, $width, $height);
$WaterMarkText = 'CONFIDENTIAL';
$watermarkColor = imagecolorallocate($imageProperties, 191,191,191);
imagestring($imageProperties, 5, 130, 117, $WaterMarkText, $watermarkColor);
imagepng($imageProperties, 'bbimage_3.png');
header('Content-type: image/jpeg');
imagepng ($imageProperties);
imagedestroy($targetLayer);
imagedestroy($imageProperties);
?>
imagecopy: This function copies source image onto destination image by overwriting destination image pixels.
While merging png images with transparent background as a watermark, imagecopymerge() function will not preserve transparency onto the destination. So, imagecopy() is preferable for image watermarking.
I created a white background and add a Png image. But upon doing trails & error still the png image show light backgroung color , it should be 100% transparent with no bg color.
This is my code after doing trails and error, but it still shows a ligh background color in png image. It should be 100% transparent. I tried a lot guys but not got any solutions upto now.
$img_h = imagesy($image);
$img_w = imagesx($image);
// create new image (canvas) of proper aspect ratio
$img = imagecreatetruecolor($canvas_w, $canvas_h);
$background = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $background);
$xoffset = ($canvas_w - $img_w) / 2;
$yoffset = ($canvas_h - $img_h) / 2;
imagealphablending($img, true);
imagesavealpha($img, true);
imagecopyresampled($img, $image, $xoffset, $yoffset, 0, 0, $img_w, $img_h, $img_w, $img_h);
I expected png image will be seen transparent on white background. But the code adds a light background color on png image.
Image preview -
I have a resize image script that takes a 130x81 image and adds it to a 130x130 image, when the imagecopyresampled function runs it adds a black background into the space that is left over, even though the base image is white. Code below, I could really appreciate some help.
The image I am trying to merge onto the 130x130 file php created is:
$width = 130;
$height = 130;
$filename = 'process-add.jpg'; //130x81px jpg
$this->_image = imagecreatefromjpeg($filename);
$background = imagecreatetruecolor(130,130);//create the background 130x130
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagefill($background,0,0,$whiteBackground); // fill the background with white
imagecopyresampled($background, $this->_image,(130-$width)/2,(130-$height)/2, 0, 0, $width, $height, $width, $height); // copy the image to the background
ImageJpeg ($background,null,100); //display
I have read on multiple posts to add:
imagealphablending($background, false);
into the code which should fix it, but it doesn't make any difference.
Thanks in advance!
This has been solved. The issue was with teh width and height on the imagecopyresampled call. See the code block below:
<?
ini_set('allow_url_fopen', true);
$filename = 'http://img.yessy.com/1402152287-17201a.jpg'; // 130x81
$image = imagecreatefromjpeg($filename);
list($originalWidth, $originalHeight) = getimagesize($filename);
// Size of image to create
$width = 130;
$height = 130;
$background = imagecreatetruecolor($width, $height);//create the background 130x130
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagefill($background,0,0,$whiteBackground); // fill the background with white
imagecopyresampled($background, $image, 0, ($height - $originalHeight) / 2, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight); // copy the image to the background
header("Content-type: image/jpeg");
ImageJpeg ($background,null,100); //display
?>
Currently I would like to create a transparent png with the lowest quality .
The code:
<?php
function createImg ($src, $dst, $width, $height, $quality) {
$newImage = imagecreatetruecolor($width,$height);
$source = imagecreatefrompng($src); //imagecreatefrompng() returns an image identifier representing the image obtained from the given filename.
imagecopyresampled($newImage,$source,0,0,0,0,$width,$height,$width,$height);
imagepng($newImage,$dst,$quality); //imagepng() creates a PNG file from the given image.
return $dst;
}
createImg ('test.png','test.png','1920','1080','1');
?>
However, there are some problems:
Do I need to specific a png file before creating any new file? Or can I create without any existing png file?
Warning: imagecreatefrompng(test.png): failed to open stream: No such file or directory in
C:\DSPadmin\DEV\ajax_optipng1.5\create.php on line 4
Although there are error message , it still generate a png file , however, what I found that is the file is a black color image , do I need to specific any parameter to make it transparent?
Thanks.
To 1)
imagecreatefrompng('test.png') tries to open the file test.png which then can be edited with GD functions.
To 2)
To enable saving of the alpha channel imagesavealpha($img, true); is used.
The following code creates a 200x200px sized transparent image by enabling alpha saving and filling it with transparency.
<?php
$img = imagecreatetruecolor(200, 200);
imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);
imagepng($img, 'test.png');
Take a look at:
imagecolorallocatealpha
imagefill
An example function copies transparent PNG files:
<?php
function copyTransparent($src, $output)
{
$dimensions = getimagesize($src);
$x = $dimensions[0];
$y = $dimensions[1];
$im = imagecreatetruecolor($x,$y);
$src_ = imagecreatefrompng($src);
// Prepare alpha channel for transparent background
$alpha_channel = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagecolortransparent($im, $alpha_channel);
// Fill image
imagefill($im, 0, 0, $alpha_channel);
// Copy from other
imagecopy($im,$src_, 0, 0, 0, 0, $x, $y);
// Save transparency
imagesavealpha($im,true);
// Save PNG
imagepng($im,$output,9);
imagedestroy($im);
}
$png = 'test.png';
copyTransparent($png,"png.png");
?>
1) You can create a new png file without any existing one.
2) You get a black color image because you use imagecreatetruecolor();. It creates a highest quality image with a black background. As you need a lowest quality image use imagecreate();
<?php
$tt_image = imagecreate( 100, 50 ); /* width, height */
$background = imagecolorallocatealpha( $tt_image, 0, 0, 255, 127 ); /* In RGB colors- (Red, Green, Blue, Transparency ) */
header( "Content-type: image/png" );
imagepng( $tt_image );
imagecolordeallocate( $background );
imagedestroy( $tt_image );
?>
You can read more in this article: How to Create an Image Using PHP