PHP saves the png file as a 0 byte to disk - php

My intention is save the generated qr code image on my local.
I have already checked out whole stackoverflow question about it. However they didn't help me to solve this bug.
<?php
header('Content-type: image/png');
$filename = "./qrs/qr-6234/qr.png";
$link = "https://stackoverflow.com";
$size = 250;
$url = urlencode ( $link );
$qr_url = "http://chart.googleapis.com/chart?chs=$sizex$size&cht=qr&chld=L|0&chl=$url&choe=UTF-8";
$qr = file_get_contents($qr_url);
$imgIn = imagecreatefrompng ($qr);
$imgOut = imagecreate ( $size, $size );
imagecopy ( $imgOut, $imgIn, 0, 0, 0, 0, $size, $size );
imagepng ( $imgOut, $filename, 9);
imagedestroy ( $imgIn );
imagedestroy ( $imgOut );
?>
I don't know why but, it gives me zero byte png file.
Edit: Thanks to ishagg, I got my error logs. These are ;
Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ./qr/qr_txt_test.php on line 10
Warning: file_get_contents(http://chart.googleapis.com/chart?chs=250&cht=qr&chld=L|0&chl=https%3A%2F%2Fstackoverflow.com&choe=UTF-8): failed to open stream: no suitable wrapper could be found in ./qr/qr_txt_test.php on line 10
Warning: imagecreatefromstring(): Empty string or invalid image in ./qr/qr_txt_test.php on line 11
Warning: imagecopy() expects parameter 2 to be resource, boolean given in ./qr/qr_txt_test.php on line 13
Warning: imagepng(): gd-png error: no colors in palette in ./qr/qr_txt_test.php on line 14
Warning: imagedestroy() expects parameter 1 to be resource, boolean given in ./qr/qr_txt_test.php on line 15

If you remove the header() call, you'll be able to see the errors in the script.
In yours, there are two:
Variable $sizex, used in $qr_url is undefined.
To create a new image from an external resource, you need to use imagecreatefromstring().
With these two mistakes fixed, the code works correctly:
<?php
header('Content-type: image/png');
$filename = "qr.png";
$link = "https://stackoverflow.com";
$size = 250;
$url = urlencode ( $link );
$qr_url = "http://chart.googleapis.com/chart?chs=$size&cht=qr&chld=L|0&chl=$url&choe=UTF-8";
$qr = file_get_contents($qr_url);
$imgIn = imagecreatefromstring($qr);
$imgOut = imagecreate ( $size, $size );
imagecopy ( $imgOut, $imgIn, 0, 0, 0, 0, $size, $size );
imagepng ( $imgOut, $filename, 9);
imagedestroy ( $imgIn );
imagedestroy ( $imgOut );
Result:

Related

Trying to transparent an image php

I'm again. I need rlly help, i need put transparent an img php
But i can't get it. I'm rlly newbie on PHP and I want learn. I was reading on PHP.net and I get only remove some problems.
Here's the code:
<?php
header('Content-type: image/jpeg');
$im = file_get_contents("http://fthw.cf/count/index.php?dt=2016-02-13/10:00:00");
$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $black);
imagepng($im, '$img');
imagedestroy($im);
?>
ERROR LOG:
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagecolorallocate() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 4
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagecolortransparent() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 5
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagepng() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 6
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagedestroy() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 7
Greetings!
PS: help me pls
<?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 = 'file.png';
copyTransparent($png,"png.png");
?>
Here is a function you can use to make a image transparent

How to resize image using Gd library ? PHP

after receiving the image from the client , I would like to resize it and then store it .
I am using this function:
function PIPHP_ImageResize($image, $w, $h)
{
$oldw = imagesx($image);
$oldh = imagesy($image);
$temp = imagecreatetruecolor($w, $h);
imagecopyresampled($temp, $image, 0, 0, 0, 0,
$w, $h, $oldw, $oldh);
return $temp;
}
$image = PIPHP_ImageResize($_FILES['file']['tmp_name'],10,10);
move_uploaded_file($image, $newname);
unfortunately I got these warning:
Warning: imagesx() expects parameter 1 to be resource, string given...
Warning: imagesy() expects parameter 1 to be resource, string given ...
Warning: imagecopyresampled() expects parameter 2 to be resource, string given ...
Warning: move_uploaded_file() expects parameter 1 to be string, resource given ...
How could I fix the problem !!
imagesx expect an image resource as first parameter. You have to create one using the appropriate function, imagecreatefromjpeg or imagecreatefrompng for example.
function PIPHP_ImageResize($image, $w, $h)
{
$oldw = imagesx($image);
$oldh = imagesy($image);
$temp = imagecreatetruecolor($w, $h);
imagecopyresampled($temp, $image, 0, 0, 0, 0, $w, $h, $oldw, $oldh);
return $temp;
}
if (move_uploaded_file($_FILES['file']['tmp_name'], $newname)) {
$uploadedImage = imagecreatefromjpeg($newname);
if (!$uploadedImage) {
throw new Exception('The uploaded file is corrupted (or wrong format)');
} else {
$resizedImage = PIPHP_ImageResize($uploadedImage,10,10);
// save your image on disk
if (!imagejpeg ($resizedImage, "new/filename/path")) {
throw new Exception('failed to save resized image');
}
}
} else {
throw new Exception('failed Upload');
}
I've added a minimal and insufficient error handling, you should check, for example, the format of the uploaded file and use the appropriate image creator function, or check if the uploaded file is an image.
Addendum
You can determine the type of the presumed uploaded image using getimagesize function.
getimagesize return an array. If the image type is supported the array[2] value return the type of the image. if the file is not an image, or its format is not supported, the array[0] value is zero. Once you know the file format of the image you can use one of the imagecreatefromxxx functions to create the appropriate image.

Warning: imagejpeg() [function.imagejpeg]: Unable to open [filename] for writing: No such file or directory

I can't figure out why I'm receiving this error when trying to save an image using imagejpeg()
Below is an excerpt from my code where I've eliminated anything extraneous and kept enough to generate the error. The path definitely exists and it has permissions set to 777.
$url = "http://website.com";
$filename = 'imagename';
$filepath = $url."/Images/accents/generated/".$filename.".jpg";
$base = imagecreatefromjpeg($url.'/images/'.$filename);
$imagewidth = imagesx($base);
$imageheight = imagesy($base);
$new = imagecreatetruecolor($imagewidth, $imageheight);
imagecopy($new, $base, 0, 0, 0, 0, $imagewidth, $imageheight);
imagejpeg($new, $filepath);
imagedestroy($new);
`
This is likely because you're trying to output to a url as opposed to a local document...
Your ouput path:
http://website.com/Images/accents/generated/imagename.jpg
This won't work... The output has to be local, try something like:
./Images/accents/generated/imagename.jpg

imagecreatefromjpeg parameter not working

I have used imagecreatefromjpeg("1.jpg") it is showing
Warning: imagecopy() expects parameter 1 to be resource, string given in E:\wamp\www
I am not understanding why it is not working and it tacks as a string, though it is a existing image name on the same file.
$new_image = "assets/small/i/m/".imagecreatetruecolor($size_width, $size_height);
$file = "upl_13541718681354171868The-best-top-desktop-dolphin-wallpapers-hd-dolphins-wallpaper-3.jpg";
$image = imagecreatefromjpeg($file);
imagecopy($new_image, $image, 0, 0, 0, 0, $size_width, $size_height);
imagejpeg($imageOutput, $outputPath, 100);
imagecreatefromjpeg
Returns an image resource identifier on success, FALSE on errors.
so you are probably passing false to
imagecopy
edit
sorry, parameter 1 is the destination. your're passing a string instead of a resource:
From docs:
// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);

getting jpeg error

<?php
function LoadPNG()
{
/* Attempt to open */
//require_once 'resizex.php';
$imgname="/home2/puneetbh/public_html/prideofhome/wp-content/uploads/268995481image_11.png";
//$im = #imagecreatefrompng($imgname);
$img= imagecreatefromstring(file_get_contents($imgname));
//$im=imagecreatefrompng('images/frame.png');
$im= imagecreatefromjpeg('images/frame.jpeg');
//imagealphablending($img, false);
//imagesavealpha($img, true);
//$img=resizex("$url",60,65,1);
imagecopymerge($im,$img,105,93,0, 0,275,258,100);
/* See if it failed */
if(!$im)
{
/* Create a blank image */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an error message */
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
$img = LoadPNG();
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
imagedestroy($img);
?>
i am getting error
arning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /home2/puneetbh/public_html/prideapp/frame.php on line 11
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'images/frame.jpeg' is not a valid JPEG file in /home2/puneetbh/public_html/prideapp/frame.php on line 11
Warning: imagecopymerge(): supplied argument is not a valid Image resource in /home2/puneetbh/public_html/prideapp/frame.php on line 16
Warning: Cannot modify header information - headers already sent by (output started at /home2/puneetbh/public_html/prideapp/frame.php:11) in /home2/puneetbh/public_html/prideapp/frame.php on line 34
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home2/puneetbh/public_html/prideapp/frame.php on line 35
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home2/puneetbh/public_html/prideapp/frame.php on line 36
The root of the problem is
'images/frame.jpeg' is not a valid JPEG file
maybe the file is broken, maybe it is a CMYK image.
You should check whether imagecreatefromjpeg() returns false, and stop execution of the script in that case and maybe output an error message.

Categories