I have binary file where is stored image, i try get this image, but display only black color image, what can be wrong with my code or binary file.
<?php
function LoadPNG ($imgname) {
$im = #imagecreatefrompng ($imgname);
if (!$im) {
$im= imagecreate (150, 30);
$bgc = imagecolorallocate ($im, 255, 255, 255);
$tc= imagecolorallocate ($im, 0, 0, 0);
imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
imagestring ($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
header('Content-Type: image/png');
$img = LoadPNG('452');
imagepng($img);
imagedestroy($img);
?>
with this code I get I error that can't load file
File: testams.serveriai.lt.lazdynas.serveriai.lt/452
Script: testams.serveriai.lt.lazdynas.serveriai.lt/crypt.php
Your PNG image is corrupt, it has a \n character instead of \r\n, (byte position 5) typically a problem arising from FTP transfering a binary image in text mode from Windows to Unix.
Before messing with PHP, you should check simply that the image is OK, eg adding the .png extension, placing it in a visible folder (in the web server) and browsing it.
Related
I'm trying to play around with GD, and I'm trying to get it to work with large images. I want a image that's originally 640x640 to resize to 130x130 on my image that I'm creating in GD. However, with my code it just crops 130x130 of the image from the upper left corner. In other words, I don't get the whole image in 130x130. I've been trying every snippet I could find, but still no luck in getting a hold of this. This is the code I have;
header ("Content-type: image/jpeg");
$image1Url = "background.jpg";
$image2Url = "image.jpg";
$image1 = imageCreateFromjpeg($image1Url);
$image2 = imageCreateFromjpeg($image2Url);
imagecopymerge($image1, $image2, 10, 10, 0, 0, 130, 130, 100);
$line1 = "This is the first line";
$line2 = "This is the second line";
$font = "./VERDANA.TTF";
$white = imagecolorallocate($image1, 255, 255, 255);
$yellow = imagecolorallocate($image1, 252, 205, 5);
imagefttext($image1, 14, 0, 150, 110, $yellow, $font, $line1);
imagefttext($image1, 14, 0, 150, 135, $white, $font, $line2);
Imagejpeg ($image1, NULL, 100);
ImageDestroy ($image1);
ImageDestroy ($image2);
I want the image specified as $image2Url to be scaled down to 130x130 no matter what size it's originally is. It's important to me that I maintain the aspect ratio though.
I've been trying different snippets I could find, but still no luck... I've been able to resize the original image to the size I want, but not within the final image in my GD script.
If you're using PHP version >= 5.5 you should use imagescale(). If not, use the following right after loading $image2:
$image3 = imagecreatetruecolor(130,130);
list($image2w, $image2h) = getimagesize($image2Url);
imagecopyresampled($image3, $image2, 0, 0, 0, 0, 130, 130, $image2w, $image2h);
// then use $image3 instead of $image2
I have a PHP source code to draw an image (a dynamic image to draw a captcha).
It was displaying a png file dynamically generated.
But it is not displayed after I moved the php to a free web server that supports PHP 5.x,
instead of my company's old PHP 4.x server.
I had changed .htaccess file to make 'html' file contain 'php' code before.
Do I need to change or add the settings in .htaccess to display 'php' as an image file in the new server?
It does not seem work with just a line:
header('Content-Type: image/png');
in the php file.
I added the php file source code below for your reference:
<?php
session_start();
// Create the image
$im = imagecreatetruecolor(150, 50);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 198, 198, 198);
//$black = imagecolorallocate($im, 0, 0, 0);
$textcol = imagecolorallocate($im, 38, 38, 38);
imagefilledrectangle($im, 0, 0, 399, 129, $gray);
// The text to draw
$text = empty($_SESSION['security_number']) ? 'error' : $_SESSION['security_number'];
$font = 'Georgia.ttf';
// Add some shadow to the text
//imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 30, 0, 10, 35, $textcol, $font, $text);
imagepng($im);
imagedestroy($im);
?>
You have to add header('Content-Type: image/png'); before you generate PNG in PHP to make it display PNG (similar for JPG).
Also, you have to check whether imagepng() successfully generates the PNG before setting header. More importantly, you have to verify whether GD Library is installed or not.
Also, it does not make sense that using .htaccess to make HTML file contains PHP codes; Use PHP extension instead.
How i can generate the below image using php
I used the below php code to generate the image
<?php
header("Content-Type: image/png");
$im = imagecreate(300, 300);
$background_color = imagecolorallocate($im, 21, 125, 126);
$black = ImageColorAllocate($im, 0, 0, 0);
$blue = ImageColorAllocate($im, 0, 0, 255);
$white = ImageColorAllocate($im, 255, 255, 255);
ImageFilledEllipse($im, 50, 120, 75, 75, $white);
ImageFilledEllipse($im, 250, 100, 75, 75, $white);
imagepng($im);
imagedestroy($im);
?>
But i dint get the original image smoothness
PHP Generated Image
Apply filter to your image using imagefilter() with the IMG_FILTER_SMOOTH argument.
imagefilter($im, IMG_FILTER_SMOOTH,100);
imagecreate creates a palette-based image, which does not include anti-aliasing (in particular because you haven't allocated the "in-between" colours). Try imagecreatetruecolor instead.
If you are creating an image of a particular size then no matter how you do it, when you zoom into such an image you are always going to get unsmooth edges.
The problem is you are zooming in on the browser after the PHP image has been rendered. Why not use a vector based library in javascript to draw your images, that way when you zoom in the vector graphic is redrawn for the appropriate scale. Eliminating your jagged edges.
I am creating image using GD library all the functions are working fine. But the main problem where i stucked that i want to merge png image over an other image but after overlapping it cannot merge properly and looking like jpg or other instead of png. I cannot upload my image here due to low reputation so click on these links below to see the image.
The image which i want to merge is this
Png image
The image where i merge above image is:
My code is here:
<?php
$im = imagecreate(288,288);
$background_color = imagecolorallocate($im, 230, 248, 248);
$file = 'images/smiley/smile'.$_POST['smiley'].'.png';
$bg = imagecreatefrompng($file);
imagealphablending($im, true);
imagesavealpha($bg, true);
imagecopyresampled($im, $bg, 80, 80, 0, 0, 50, 50, 185, 185);
header("Content-Type: image/png");
$filename = $_SESSION['rand'].'.png';
imagepng($im,$filename);
echo '<img src="'.$filename.'" alt="" />';
?>
Your background image doesn't have an alpha channel. This makes the PHP GD library do all of it's copying operations without using an alpha channel, instead just setting each pixel to be fully opaque or transparent, which is not what you want.
The simplest solution to this is to create a new image of the same size as the background that has an alpha channel, and then copy both the background and face into that one.
$baseImage = imagecreatefrompng("../../var/tmp/background.png");
$topImage = imagecreatefrompng("../../var/tmp/face.png");
// Get image dimensions
$baseWidth = imagesx($baseImage);
$baseHeight = imagesy($baseImage);
$topWidth = imagesx($topImage);
$topHeight = imagesy($topImage);
//Create a new image
$imageOut = imagecreatetruecolor($baseWidth, $baseHeight);
//Make the new image definitely have an alpha channel
$backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127);
imagefill($imageOut, 0, 0, $backgroundColor);
imagecopy($imageOut, $baseImage, 0, 0, 0, 0, $baseWidth, $baseHeight); //have to play with these
imagecopy($imageOut, $topImage, 0, 0, 0, 0, $topWidth, $topHeight); //have to play with these
//header('Content-Type: image/png');
imagePng($imageOut, "../../var/tmp/output.png");
That code produces this image:
I'm using imagettftext to draw text on an image using PHP GD
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$text = 'Test';
$font = 'data/font/arial.ttf';
imagettftext($im, 20, 0, 10, 20, $white, $font, $text);
imagepng($im);
imagedestroy($im);
It's working fine but the problem is that the font file handle is not closed after imagedestroy($im); (inspected with procexp)
And as i expected, I got a Permission denied warning when I tried to unlink the font file.
How to release the used font file? Thank you
I'm testing on windows xampp