PHP PNG header information

Discussion in 'OT Technology' started by noon, Jul 11, 2007.

  1. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    OK, so I am writing a dynamic image watermarking function in PHP

    the problem comes when people right click on a dynamic image. if I use image/jpeg for the content-type and imagejpeg to create the image, it shows the filename correctly (xxx.jpg) and the file type says JPEG Image (*.jpg)

    however, if i use image/png for the content-type and imagepng to create the image, it shows the filename correctly, but the file type field is blank. does anyone know a way to fix this?

    my headers look like this..

    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $timestamp).' GMT');
    header('Content-type: image/jpeg');
    header('Content-Disposition: inline; filename='.$imagename);

    ...

    imagejpeg($image, NULL, 95);

    This loads correctly, and shows JPEG Image for the file type, however..

    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $timestamp).' GMT');
    header('Content-type: image/png);
    header('Content-Disposition: inline; filename='.$imagename);


    imagepng($image, NULL, 95);

    Doesn't work :(


    any ideas?
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    hmmm, i did one of these a long time ago, lemme see if i can dig up the code
     
  3. retorq

    retorq What up bitch??

    Joined:
    Dec 14, 2006
    Messages:
    6,061
    Likes Received:
    0
    Location:
    Mohave Desert
    Here's mine, I modified one I found on the web:

    Code:
    <?php
    $watermark = "watermark.gif";
    $image = $_SERVER["QUERY_STRING"];
    if (empty($image)) die();
    $outputType = getFileType($image);
    watermark($image, $watermark, $outputType);
    /**
       Outputs the image $source with $watermark in the lower right corner.
       @param $source the source image
       @param $watermark the watermark to apply
       @param $outputType the type to output as (png, jpg, gif, etc.)
                          defaults to the image type of $source if left blank
    */
    function watermark($source, $watermark, $outputType="") {
       $sourceType = getFileType($source);
       $watermarkType = getFileType($watermark);
       if (empty($outputType)) $outputType = $sourceType;
       if ($outputType == "gif") $outputType = "png"; 
     
       header("Content-type:image/$outputType");
       // Derive function names
       $createSource = "ImageCreateFrom".strtoupper($sourceType);
       $showImage = "Image".strtoupper($outputType);
       $createWatermark = "ImageCreateFrom".strtoupper($watermarkType);
       // Load original and watermark to memory
       $output = $createSource($source);
       $logo = $createWatermark($watermark);
       ImageAlphaBlending($output, true);
       // Find proper coordinates so watermark will be in the lower right corner
       $x = ImageSX($output) - ImageSX($logo);
       $y = ImageSY($output) - ImageSY($logo);
       // Display
       ImageCopy($output, $logo, $x, $y, 0, 0, ImageSX($logo), ImageSY($logo));
       $showImage($output);
       // Purge
       ImageDestroy($output);
       ImageDestroy($logo);
    }
    function getFileType($string) {
       $type = strtolower(eregi_replace("^(.*)\.","",$string));
       if ($type == "jpg") $type = "jpeg";
       return $type;
    }
    ?>
    I'm not using imagejpg/png in this code, I do use it in my gallery software though if you need examples of how to use that.

    I have an .htaccess file that redirects all calls not from my domain to this file and it passes the original filename as a param . . .

    Code:
    RewriteEngine on
    RewriteRule \.(jpeg|jpg|gif|png)$ wrapper.php?%{REQUEST_FILENAME}
    Options All -Indexes
     

Share This Page