PHP's imagejpeg function isn't outputting an image - php

I'm using PHPs imagecreatefromjpeg to load a jpeg from disk and imagejpeg to output that image to the browser. For some reason though, this function is only outputting the image when the server is running on a Windows development box. On our Linux server, its not outputting any data (ie. Content-Length: 0, nothing is displayed).
Are there any workarounds that make this work on a Linux box? I've confirmed that the GD library is installed with jpeg support.

There are two notes on http://php.net/imagejpeg which may be relevant:
Notes
Note: JPEG support is only available
if PHP was compiled against GD-1.8 or
later.
Note: If you want to output
Progressive JPEGs, you need to set
interlacing on with imageinterlace().
Also some actual code would be useful in attempting to help.

Make sure you have the correct file permissions set. Does your web service own the file? Does it have permission to read or write the file?

Related

Recreate image with ffmpeg safe?

I want to use ffmpeg to recreate an image file that is uploaded. I know that in PHP you can use imagecreatefromjpeg, imagecreatefrompng and imagejpeg etc. to recreate images. That way, if there is any hidden malware in the original images, it will 'break' / be mangled.
But I want to use this ffmpeg command to recreate image files:
ffmpeg -i in.jpg out.jpg
I have tested converting an image file that has PHP code stored in its EXIF field, and after converting it, the PHP code is no longer executed in the converted file..
Also, the converted file is smaller in size: from 45.9 KB to 11 KB..
Is that a safe way?
EDIT
Btw, I am also checking the MIME type etc, but MIME can be forged, hence recreating image files is recommended.. I want to convert files on another server where there is no PHP installed. That way I can also avoid any potential PHP memory issues.

PHP getimagesize without URL file-access

I am using getimagesize in PHP to get information about an image in my script...
$thumb = "http://i.ytimg.com/vi/u6XAPnuFjJc/hqdefault.jpg";
$imageInfo = getimagesize($thumb);
The problem is that on one of my servers, it is returning the error:
getimagesize() [function.getimagesize]: URL file-access is disabled in the server configuration
I will not be able to fix this configuration issue on this specific server. I'm wondering if anyone has any ideas on how I can work around this issue. Would it be possible to take the image, save it to a temporary file and then use getimagesize based on the temporary image? Once its all done, the temporary file would then need to be deleted.
If this is possible, how would I do this?
Since you're to able to change server configuration, check if CURL is enabled.
If it is, then you can use CURL to copy the image locally and run getimagesize on it.
http://php.net/manual/en/book.curl.php
Sounds like you have fopen() URL wrappers disabled allow_url_fopen = off.
You will need to use something like cURL to download it, and then run list($width, $height) = getimagesize() on it.
If CURL is not enabled, you can still copy the image locally using sockets and an HTTP request - you can also use this to check the filesize (Content-Length header) to make sure your script doesn't download massive files (someone tried to DOS my site by doing that XD)

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');

pdf errors with php, apache2

I'm seeing some strange behavior from some dynamically generated PDFs using the TCPDF library in PHP.
Standard lamp stack-- however, if you try to open the PDF from Windows with Acrobat Reader, it gives an error that the "file is damaged and could not be repaired". From Mac, Linux, etc. the file works fine, and opens fine. It also opens fine in Google Docs-- so clearly the PDF itself is ok.
Is it possible that the mime type (application/pdf) is causing problems in Windows?
Thanks
What browser on Windows? All? Or just one? My initial gut instict, is that the Windows browser is ignoring the encoding IF the content is being gzipped (Ignoring the Content-Encoding header). That's if you're even sending that header.
Open the file you downloaded in some text editor (Notepad, etc). The first few characters of the file should be %PDF-1. with another number after it. If it's not at the beginning, check to see if the file is gzipped (rename the file to blah.gz, and then run it through gzip to try to decode it). If that worked, then your problem is the browser ignoring the encoding.
IF it is ignoring the encoding, you need to not gzip the output of that PHP file. How you do that will depend on your server configuration.
Oh, and the application/pdf is the proper mime type. And the mime type is not your problem, since Acrobat is at least trying to open the file...

php imagejpeg mime type

I am using php's imagejpeg to save a GD image resource to a file, doing this:
imagejpeg($im, '../images/' . $image_id . '.jpg');
It works fine, but according to my browser, it tries to read the file as text/plain:
Resource interpreted as image but transferred with MIME type text/plain.
Is there a step before saving the file that I am supposed to do to make sure it's using the right mine-type?
I am using windows (XAMPP), could it be a Windows issue?
EDIT: nope. I just tested in a linux server.
As far as the actual displaying, it's just plain html .
My upload code is supposed to saves= the file as a plain jpeg in the server. It's just not saving it with the right mime type.
Thanks
AFAIK, the Apache server - in standard out of the box configuration - should sent content-type headers purely based upon file extension. Apache shouldn't even even be looking at the contents or how it was originally generated/stored.
On my out-of-the-box Apache2, the file conf/mime.types contains the line:
image/jpeg jpeg jpg jpe
which ought to do it, right?
Can you post a test-case, say, a simple html page with two img tags: one for your generated image, and one for a standard image that seems to work fine?
One last thought: Does it occur in all browsers? Maybe it's a browser issue, not a server one?
It sounds like you're dumping the contents of the file to the browser and not actually telling the browser what type of file it is. Try adding a Content-type header before you output your image to the browser:
header('Content-type: image/jpeg');
Are you sure you aren't using the wrong file-extension name?
Otherwise, just put an normal image in the server and make sure the mime-types are properly configured.
It could also be that your image data from the manipulation is corrupted.

Categories