Convert an image to fake Sepia with PHP

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:

// From
// Copyright 2008, Michael Moore
// You may use this code for any purpose

function pseudosepia(&$im){
 imagefilter($im,IMG_FILTER_COLORIZE, 90, 55, 30);

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

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



This entry was posted in Programming, Projects and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *