Problem with imagecreatefrompng (PHP) - php

in one of my PHP projects, imagecreatefrompng complains, that the image "is not a valid PNG file".
Warning: imagecreatefrompng() [function.imagecreatefrompng]:
'/.../...png' is not a valid PNG file in /.../...php on line ...
I'm pretty sure, the file is valid, because I can open it in any program. So I was wondering what else could cause this warning.
Does anybody know the details behind imagecreatefrompng or has had a similar problem before?
Thanks for your help,
Timo

Is it a PNG image? Run file against it, or try the fileinfo functions.

Check this mime type of file you are going to upload by using below function:
mime_content_type($_FILES['product_img']['tmp_name'])
Image editors can often recognize file type by its contents, php might just try to use the file based on its mime-type. If it don't match then there's an error.

Related

FPDF issue while generating pdf with images

require('fpdf/fpdf.php');
$pdf->AddPage("P", "A4");
$pdf->SetDisplayMode(150,'default');
$pdf->Image($imagepath,60,30,90,0,'JPG');
b_end_clean();
$pdf->Output();
('FPDF error: Not a JPEG file: https://myurl.com')
Use the image from local folder that is always best.
For this kind of error only one suggestion even though it have the extension of jpeg clearly they are not jpeg image.Me also face the same problem. Just download one jpg image from google and try this code sure it work.
Try loading it in your image editor and saving it again.
I had the same issue and happened because I only renamed the file.

Changing the file extension using PHP

Is it "good" to change an image file extension?
For instance, I have a jpg file inputed, and I change it to a png. Is this OK or should I leave it as a jpg?
I use the rename() to change the name and the file extension.
It is related to PHP, because I do my renaming with PHP with a upload script.
Another question is: Is it safe to do it e.g can the files become corrupt?
Changing an image file extension from .jpg to .png does not change it to a PNG file. It just changes the name of the file. This does not change the contents of the file at all, so it does not become corrupt. It only changes the name of it.
Leave file names with the appropriate extension, or you will confuse everyone, including yourself.
No, the file cannot become corrupt... a file name is just the name.
Never allow users to dictate the names of files on your system. No matter what they upload, rename it to something with no extension, and store the files outside of the web server's doc root. You don't want them uploading .php scripts and what not.
Changing the extension of file is just renaming it. The extension serves the purpose of merely defining the data type that the file contains.
Renaming a JPEG file into a PNG is therefore a very bad idea. Although some image viewers may still be able to figure out that your PNG is actually JPEG and view it correctly, others will not.
Use ImageMagick or GD 2 to do any image conversions in PHP.

'ImagickException' with message 'unable to open file

'ImagickException' with message 'unable to open file /data/web/myweb.com/sub/7/file:/data/web/myweb.com/sub/7/sites/default/files/logo.png'
Hi,
I got this message when I want to crete PDF file from the webpage, that png files. It throws error only when content of PDF contains some PNG file. When I remove all png files, that should be in pdf, everything is correct.
When I look in php info, I see png is supported format for Imagick. When I tried to find solution on google, it returned many of sites with the same error but almost no solution. the only solution I have found is to install another library to server. But I would prefer another solution if there is any.
thnaks for any advice
Tomas
ImageMagick can indeed take png files and convert it to pdf. Are you sure the file is in fact on the server at the correct place.
it feels like the inout command to ImageMagick may be incorrect. The path below looks weird.
data/web/myweb.com/sub/7/file:/data/web/myweb.com/sub/7/sites/default/files/logo.png
^^^^^
Is this expected?
I can tell you more if you tell me the website. Otherwise, I would say its a bug in the website php/python script. It doesn't look like an ImageMagick issue to me.
The ImagickException error message indicates that Imagick (PHP class) is choking on the 'file://' prefix, which is a streamwrapper, fairly recent PHP addition. Imagick documentation is not clear on the use of stremwrappers, and it is most certain that Imagick does not support them. When it sees no '/' in the first byte of the file name, it assumes the file name is relative and pre-pends current directory to it, creating the monster string you pasted in the question.
Solution is to trim the 'file://' prefix on the file name you are sending to the Imagick. File name should be either absolute (start with /) or relative.
if (strpos('file://', $filename) === 0) {
$filename = substr($filename, 7);
}

Verifying file type of ANY upload

I read that for images, it's not safe to depend on the file extension and that it's better to try to open the php with an image library like gd to verify its extension.
What about other types of files? If I have a .doc or .pdf or any other file type, how can I really tell the file type is really what it claims it is?
If you are on a *nix system the file command does a pretty good job at guessing mime type. It is not perfect, and fails on 'nested' types like .tar.gz but it is pretty good.
As i understand it Fileinfo uses the same magic numbers approach as file without needing to go to the shell...
I don't know if it works for any file type, but you can check mime type using mime_content_type or filetype.

getimagesize function does not working

Images in my website is located in other server. I tried to get the size of an image using getimagesize of php and it is not working. It gives an error 'getimagesize is not seekable'. I tried this same function from other server(images are in same server and I change the location of the file, which is using getimagesize function) and it worked. Can anyone solve this?
From the manual:
filename
This parameter specifies the file you wish to retrieve information about. It can reference a local file or (configuration permitting) a remote file using one of the supported streams.
The settings they're talking about (for the HTTP wrappers) is for the most part having allow_url_fopen enabled or disabled.
The error is probably could not make seekable and could mean that PHP / GD cannot recognize the image file as such.
Have you tried with different images and different image formats (GIF, JPG, PNG) ?
Maybe GIF or PNG is not supported by your PHP version.
maybe the image path is wrong
try using this instead:
dirname($_SERVER['SCRIPT_FILENAME'])
it will return the parent directory's path,
example:
getimagesize(dirname($_SERVER['SCRIPT_FILENAME']).'/images/sampleimage.jpg');

Categories