Image Mime Type mystery in Drupal

I have a development Drupal 6.19 / WAMP setup that’s giving me some headaches. Among the mysteries are some apparently broken image paths that appear to be pointing to the correct locations/files. On inspecting the image in Chrome developer tools (under the “resources” tab), I find: mime-type = application/x-httpd-php.

I have:

  • image/png png defined in file mime.types
  • mime.types is included in httpd.conf via TypesConfig conf/mime.types
  • since it’s a WAMP server we also have LoadModule mime_module modules/mod_mime.so
  • and finally the php.ini has only default_mimetype = “text/html”

Apache directory structure:

\\Apache Software Foundation 
     \\Apache2.2
          \\conf
               httpd.conf
               mime.types

Note that httpd.conf & mime.types are in the same directory.

Am I missing something here? Is the path to mime.types supposed to be relative to httpd.conf, or to the PHP root folder? or is the problem in Drupal?

You’re trying to access $image[‘mime’] but $image is the actual GD image resource.

Try this:


define('MIN_WIDTH', 225);
define('MIN_HEIGHT', 225);

$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$image = imagecreatefromstring(file_get_contents($path));
$image_data = getimagesize($path);

$w = imagesx($image);
$h = imagesy($image);

if ($w >= MIN_WIDTH && $h >= MIN_HEIGHT) {
    $watermark = imagecreatefrompng('watermark.png');
    $ww = imagesx($watermark);
    $wh = imagesy($watermark);
    imagecopy($image, $watermark, (($w/96)-($ww/96)), (($h/96)-($wh/96)), 0, 0, $ww, $wh);
    header('Content-Type: ' . $image_data['mime']);
    imagepng($image,null,0,PNG_NO_FILTER);
    exit();
} else {
    imagecopy($image);
    header('Content-Type: ' . $image_data['mime']);
    imagepng($image,null,0,PNG_NO_FILTER);
    exit();
} 


Check the bit I’ve added for $image_data.

I just tried that — while it works fine on the production server (which was working OK anyway), it still doesn’t seem to work on the testing server. Here’s the full code:

define('MIN_WIDTH', 225);
define('MIN_HEIGHT', 225);

$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];

$image = imagecreatefromstring(file_get_contents($path));

$w = imagesx($image);
$h = imagesy($image);

if ($w >= MIN_WIDTH && $h >= MIN_HEIGHT) {
	$watermark = imagecreatefrompng('watermark.png');
	$ww = imagesx($watermark);
	$wh = imagesy($watermark);
	
	imagecopy($image, $watermark, (($w/96)-($ww/96)), (($h/96)-($wh/96)), 0, 0, $ww, $wh);
	
	header('Content-Type: ' . $image['mime']);
	imagepng($image,null,0,PNG_NO_FILTER);
	exit();
} else {
	imagecopy($image);
	
	header('Content-Type: ' . $image['mime']);
	imagepng($image,null,0,PNG_NO_FILTER);
	exit();
}

It turns out it was an .htaccess issue. I’ve got a rewrite rule that looks for image files & runs a PHP script to watermark them. It works fine on my production server, but for some reason I haven’t figured out, bollixes the development server.

My version of the script includes the changes found in my comment to the article, namely, repositioning the watermark & using an if loop to prevent watermarks on smaller images. Oh, I guess I didn’t post the if loop. So if any one is interested, it’s just:

define('MIN_WIDTH', 225);
define('MIN_HEIGHT', 225);
...
$w = imagesx($image);
$h = imagesy($image);
...
if ($w >= MIN_WIDTH && $h >= MIN_HEIGHT) {
(do stuff);
}

The closest thing to a root folder is the “sites” folder, since I’m running a multi-site setup w/ single codebase. If I drop the PNG there, I can view it & it appears to be the correct MIME type.

No clue which PHP in Drupal might be processing the image before the browser sees it. I’m using a couple of Drupal modules (Image & Image Assist) + the ImageMagick Toolkit, so there’s a lot going on w/ it – but I can tell you, they are creating a PNG file; it’s just not accessible thru the website.

Are the images processed by PHP before being output to the server?
In that case, the code (in Drupal, I guess) that processes the images should be outputting the image/png header.

If you put a png image in your websrver’s root, and request it directly, what headers are sent?

What you could do is to add the following header to that file, it’ll stop your mime-type problems and keep the watermarking active:


$data = getimagesize($path_to_image);
header('Content-Type: ' . $data['mime']);

[strike]Beautiful. Thanks for all the advice.[/strike]Whoops, forgot to save the .htaccess – still doesn’t work.