Convert an image to fake Sepia with PHP

Posted by stuporglue on Mar 18, 2010 in Programming, Projects
Subscribe to posts by category...
Programming, Projects

It’s pretty easy to do image manipulation with PHP.  Here’s a sample of converting an image from color into sepia using PHP’s imagefilter.

Be sure that your memory_limit will handle the image sizes you want to process. @imagecreatefromjpeg will return FALSE if it can’t load the image (because of a bad file path, file size or whatever).  This isn’t a real sepia conversion, but it’s a decent fake. The image is converted to greyscale, the brightness turned down, and then colorized with a red tint.

PHP’s imagefilter has lots of other neat tricks it can do, so be sure to check it out.

The original image:

Ryan with a sword and shield

The sepia image:

A Sepia version of Ryan with a sword

The image above is generated on the fly, if you’ll check the image source for the image, it is a PHP file. As long as you send the right headers, browsers don’t care what the file extension is, at least for images.

Here’s the code:

<?php
// From http://stuporglue.org/
// Copyright 2008, Michael Moore
// You may use this code for any purpose

function pseudosepia(&$im){
 imagefilter($im,IMG_FILTER_GRAYSCALE);
 imagefilter($im,IMG_FILTER_BRIGHTNESS,-30);
 imagefilter($im,IMG_FILTER_COLORIZE, 90, 55, 30);
}

ini_set('memory_limit', '15M');
$im = @imagecreatefromjpeg('test.jpg');
$percent = '80';
pseudosepia($im);

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache,must-revalidate");
header("Cache-Control: post-check=0, pre-check=0",false);
header("Pragma: no-cache");
header("Content-type: image/jpeg");

imagejpeg($im);
?>

Enjoy!

Tell Your Friends
  • Digg
  • Facebook
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Twitter

Tags: , ,

Leave a Reply

XHTML: You can use these tags:' <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Original content on this page is under the Creative Commons Attribution-ShareAlike 3.0 License unless otherwise noted.

If you found this page particularly helpful, I'd love to hear from you. A link to this page from your blog or website would also be appreciated.

I'm the real Michael Moore