getting jpeg error - php

<?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.

Related

PHP saves the png file as a 0 byte to disk

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:

Fatal error: Uncaught exception 'ImagickException' with message 'unable to open image

I am using php Imagick for uploading cropped image using `cropImage' after that I want to resize that cropped image using 'resizeImage'. Here image cropping is working but resize image is showing exception my code is below.
upload.php
move_uploaded_file($_FILES["file"]["tmp_name"], $newUploadDir. $newfilename);
cropImage($newUploadDir.$newfilename,$newUploadDirSmall.$newfilename,$x,$y,$w,$h);
resizeImage($newUploadDirSmall.$newfilename,$newUploadDirSm.$newfilename,211, 50, 0, 0, true, false);
MagikLib.php
<?php
function cropImage($source,$destination, $startX, $startY, $width, $height)
{
$imagick = new \Imagick(realpath($source));
$imagick->cropImage($width, $height, $startX, $startY);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
$imagick->writeImage($destination);
}
function resizeImage($source,$destination, $width, $height, $filterType, $blur, $bestFit, $cropZoom)
{
//The blur factor where > 1 is blurry, < 1 is sharp.
$imagick = new \Imagick(realpath($source));
$imagick->resizeImage($width, $height, $filterType, $blur, $bestFit);
$cropWidth = $imagick->getImageWidth();
$cropHeight = $imagick->getImageHeight();
if ($cropZoom) {
$newWidth = $cropWidth / 2;
$newHeight = $cropHeight / 2;
$imagick->cropimage(
$newWidth,
$newHeight,
($cropWidth - $newWidth) / 2,
($cropHeight - $newHeight) / 2
);
$imagick->scaleimage(
$imagick->getImageWidth() * 4,
$imagick->getImageHeight() * 4
);
}
header("Content-Type: image/jpg");
$imagick->writeImage($destination);
}
?>
MagicLib.php is reffered from http://phpimagick.com/Imagick/cropImage and http://phpimagick.com/Imagick/resizeImage
cropImage is working properly and saving image to folder but resizeImage is showing following errors
Warning: Cannot modify header information - headers already sent by (output started at /home/a/public_html/img/MagikLib.php:7) in /home/a/public_html/img/MagikLib.php on line 34
Fatal error: Uncaught exception 'ImagickException' with message 'unable to open image `/home/a/public_html/img/images//b6d767d2f8ed5d21a44b0e5886680cb9/user/sm/image1.jpg': No such file or directory # error/blob.c/OpenBlob/2709' in /home/a/public_html/img/MagikLib.php:35 Stack trace: #0 /home/a/public_html/img/MagikLib.php(35): Imagick->writeimage('/home/a/pu...') #1 /home/a/public_html/img/upload1.php(74): resizeImage('/home/a/pu...', '/home/a/pu...', 211, 50, 0, 0, true, false) #2 {main} thrown in /home/a/public_html/img/MagikLib.php on line 35
How to resolve this problem?
This is usually caused by the dir is not exists.
add this code:
$destinationPath = dirname($destination);
`mkdir -p $destinationpath`;
before
$imagick->writeImage($destination);
To recursively create path.

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

Why my image created by php is not displaying?

I have a problem , I have a software that gets pc tech specs and post them online and we are offering a signature image for each specs, this is created by php , but is not working anymore;
Demo link : http://checkmyspecs.co.uk/button.php?id=646725 or go to any specs page , example : http://www.checkmyspecs.co.uk/display2.php?id=646725 and down on the page is grab button code.
The image must be created by button.php , here is the code :
<?php
include "db.php";
function getdata($viewerid) {
$query = "SELECT * FROM data where viewerid = '$viewerid'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$data = array();
array_push($data,$row['bootmethod'],$row['ComputerCaption'],$row['Infrared'],$row['DayLight'],$row['ManufacturerBox'],$row['Model'],$row['cores'],$row['memory'],$row['monitor'],$row['resolution'],$row['pixels'],$row['cpuvoltage'],$row['clockspeed'],$row['AddressWidth'],$row['SocketDesignation'],$row['cpuname'],$row['loadpercent'],$row['applications'],$row['videocardname'],$row['refreshrate'],$row['videodriver'],$row['installed'],$row['hddata'],$row['directx']);
return $data;
}
function LoadPNG($imgname,$cur)
{
$getvalues = getdata($cur);
/* Attempt to open */
$im = #imagecreatefrompng($imgname);
// Removes white background (made from the original transparency)
$white = imagecolorallocate($im, 255, 255, 255);
// Make the background transparent
//Generate the text to write onto the image (the php Version).
//Don't know much about this
// Add some shadow to the text
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$blue = imagecolorallocate($im, 0, 173, 238);
$darkblue = imagecolorallocate($im, 54, 154, 191);
$font = '/fonts/arialbd.ttf';
$ramo = $getvalues[7];
$cpuo = $getvalues[15];
$gpuo = $getvalues[18];
$screeno = $getvalues[9];
$ram = "$ramo MB";
$cpu = "$cpuo";
$gpu = "$gpuo ";
$screen = "$screeno";
imagettftext($im, 11, 0, 40, 23, $black, $font, $cpu);
imagettftext($im, 11, 0, 40, 53, $black, $font, $gpu);
imagettftext($im, 11, 0, 40, 83, $black, $font, $screen);
imagettftext($im, 11, 0, 40, 113, $black, $font, $ram);
// write text
$textcolor = imagecolorallocate($im, 0, 3, 0);
return $im;
}
header('Content-Type: image/png');
$button = '/images/button.png';
$img = LoadPNG($button,$_GET['id']);
imagepng($img);
?>
Your image isn't an image. Here's what is really being output:
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 24
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 33
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 34
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 35
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 36
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 37
Fatal error: Call to undefined function imagettftext() in /home/checkmys/public_html/button.php on line 53
You can use tools such as Fiddler to see this, even when your content-type is set as an image. If you would remove that # symbol, we could see the error describing why your image could not be created.
Also, you are currently wide open to SQL injection. You should use prepared queries with PDO to avoid this problem.

How to convert any image to JPG or JPEG using PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Save image from one format to another with php gd2
I am converting any image format to JPEG or JPG. Code for converting image to jpg is
function converttojpg($originalimg)
{
$filetype = #end(explode(".", $originalimg));
echo $originalimg . " asdasdfs";
if (strtolower($filetype) == 'jpg' or strtolower($filetype) == 'jpeg')
{
$srcImg = imagecreatefromjpeg($originalimg);
imagejpeg($srcImg,$originalimg,100);
imagedestroy($srcImg);
}
if (strtolower($filetype) == 'png')
{
$srcImg = imagecreatefrompng($originalimg);
imagejpeg($srcImg,$originalimg,100);
imagedestroy($srcImg);
}
if (strtolower($filetype) == 'gif')
{
echo "executed";
$srcImg = imagecreatefromgif($originalimg);
imagejpeg($srcImg,$originalimg,100);
imagedestroy($srcImg);
}
}
After that i resize that converted image, whose code is
function resizeImage($originalImage,$toWidth,$toHeight)
{
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;
// Recalculate new size with default ratio
if ($yscale>$xscale)
{
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else
{
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
return $imageResized;
}
and on Every image it reports errors and warning which are
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 9 extraneous bytes before marker 0xd9 in E:\xampp\htdocs\photo\functions.php on line 33
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'userphotos/corrupted_image_1.jpg' is not a valid JPEG file in E:\xampp\htdocs\photo\functions.php on line 33
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\photo\functions.php on line 34
Warning: imagedestroy() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\photo\functions.php on line 35
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 9 extraneous bytes before marker 0xd9 in E:\xampp\htdocs\photo\functions.php on line 22
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'userphotos/53.jpg' is not a valid JPEG file in E:\xampp\htdocs\photo\functions.php on line 22
Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in E:\xampp\htdocs\photo\functions.php on line 23
userphotos/MG_diesel-04.gif_thumb.png asdasdfsuserphotos/Porsche_911_996_Carrera_4S.jpg asdasdfs
Please help me in solving that problem. I have to complete it.
Don't use extension to determine file type. To check if given file is an image and in format that allows GD to manipulate it, use getimagesize
<?php
$image = #imagecreatefromstring(file_get_contents($file_path));
if ($image === false) {
throw new Exception (sprintf('invalid image or file not found, file=%s', $file_path));
}
?>
imagecreatefromstring detects image type and creates an image resource, otherwise exception is thrown.

Categories