Log in

View Full Version : Trying to create a sepia image...



munkynpunky
08-04-2007, 01:31 PM
I use this script to make my images B+W, but is there anyway to make the images appear sepia too?


<?php
// The file you are grayscaling
$file = $_GET['imageid'];

// This sets it to a .jpg, but you can change this to png or gif if that is what you are working with
header('Content-type: image/jpeg');

// Get the dimensions
list($width, $height) = getimagesize($file);

// Define our source image
$source = imagecreatefromjpeg($file);

// Creating the Canvas
$bwimage= imagecreate($width, $height);

//Creates the 256 color palette
for ($c=0;$c<256;$c++)
{
$palette[$c] = imagecolorallocate($bwimage,$c,$c,$c);
}

//Creates yiq function
function yiq($r,$g,$b)
{
return (($r*0.299)+($g*0.587)+($b*0.114));
}

//Reads the origonal colors pixel by pixel
for ($y=0;$y<$height;$y++)
{
for ($x=0;$x<$width;$x++)
{
$rgb = imagecolorat($source,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

//This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
$gs = yiq($r,$g,$b);
imagesetpixel($bwimage,$x,$y,$palette[$gs]);
}
}

// Outputs a jpg image, but you can change this to png or gif if that is what you are working with
imagejpeg($bwimage);
?>

shachi
08-04-2007, 08:09 PM
http://www.ditman.net/ps/index.php

djr33
08-04-2007, 08:21 PM
Think about sepia. It's just brown tones.

That means you will want to have (generally) a high red value, and a higher green value than blue. Aside from that, to make it darker, just increase RGB equally, and that should work.

Twey
08-04-2007, 10:47 PM
to make it darker, just increase RGB equallyDecrease, surely?

djr33
08-05-2007, 12:56 AM
Ah, yes. That would soon be found out :p

munkynpunky
08-06-2007, 09:15 AM
I'm using this script

<?php
//This class turns a color image into Sepia
class bw {

var $_bw;
var $_type;

function printImage() {
switch ($this->_type) {
case IMAGETYPE_GIF : {
header('Content-Type: image/gif');
imagegif($this->_bw);
} break;
case IMAGETYPE_JPEG : {
header('Content-Type: image/jpeg');
imagejpeg($this->_bw);
} break;
case IMAGETYPE_PNG : {
header('Content-Type: image/png');
imagepng($this->_bw);
} break;
default : {
header('Content-Type: image/jpeg');
imagejpeg($this->_bw);
} break;
} // switch
imagedestroy($this->_bw);
}

function bw($path) {
// Let's find out what type is this image:
if (@is_file($path)) {
$this->_type = @exif_imagetype($path);
switch ($this->_type) {
case IMAGETYPE_GIF : {
$img = @imagecreatefromgif($path);
} break;
case IMAGETYPE_JPEG : {
$img = @imagecreatefromjpeg($path);
} break;
case IMAGETYPE_PNG : {
$img = @imagecreatefrompng($path);
} break;
default : {
$img =_errorImg();
} break;
} // switch ($this->_type)

// If image was created successfully, do some magic stuff
if ($img) {
$this->_bw = $this->_toBW($img);
imagedestroy($img);
} // if ($this->_img)
} // if (@is_file...)
else {
$this->_bw = $this->_errorImg();
}
}
function _errorImg() {
return imagecreate(200,200);
}

function _toBW($src){
$x = imagesx($src);
$y = imagesy($src);

$dest = imagecreatetruecolor($x, $y);

for ($i=0;$i<$x;$i++) {
for ($j=0;$j<$y;$j++) {
$oldcolor = imagecolorsforindex($src, imagecolorat($src, $i, $j));
$newcolor = (int)(($oldcolor['red'] + $oldcolor['green'] + $oldcolor['blue']) / 3);
$sepia = array(
"red" => 145,
"green" => 65,
"blue" => 10
);
$mix = array(
"o" => 0.7,
"d" => 0.3
);
$newr = ($newcolor*$mix['o'] + $sepia['red']*$mix['d'])>255?255:($newcolor*$mix['o'] + $sepia['red']*$mix['d']);
$newg = ($newcolor*$mix['o'] + $sepia['green']*$mix['d'])>255?255:($newcolor*$mix['o'] + $sepia['green']*$mix['d']);
$newb = ($newcolor*$mix['o'] + $sepia['blue']*$mix['d'])>255?255:($newcolor*$mix['o'] + $sepia['blue']*$mix['d']);
imagesetpixel($dest, $i, $j, imagecolorallocate($dest, $newr, $newg, $newb ));
} // for($j
} // for ($i

return $dest;
}
}

import_request_variables('g','bw_');

$bw = new bw($bw_im);
$bw->printImage();

?>


I tried to pass the image as $this, but I just get a load of scribble on the page, how do i tell it which image to make sepia?

any thoughts - im still a newbie :)

munkynpunky
08-06-2007, 09:18 AM
Think about sepia. It's just brown tones.

That means you will want to have (generally) a high red value, and a higher green value than blue. Aside from that, to make it darker, just increase RGB equally, and that should work.

Yeah i was thinking that, however I could not see on the original script where to set the RGB codes... there wasnt a really clear way to do it:confused:

djr33
08-06-2007, 10:08 AM
Here's something you could play with--

<?php
function graytosepia($im) {
for ($x=0; $x<imagesx($im);$x++) {
for ($y=0; $y<imagesy($im);$y++) {
$c = imagecolorsforindex($im,imagecolorat($im,$x,$y));
$r = 1.5*$c['red'];
$g = 1*$c['red'];
$b = 0.5*$c['red'];
imagesetpixel($im,$x,$y,imagecolorallocate($im,$r,$g,$b));
}
}
}
?>

Might want to look here:
http://www.php.net/manual/en/function.imagefilter.php
Note IMG_FILTER_COLORIZE

munkynpunky
08-06-2007, 11:32 AM
Is there anyway to edit this to get it displaying sepia?


<?php
// The file you are grayscaling
$file = $_GET['image'];

// This sets it to a .jpg, but you can change this to png or gif if that is what you are working with
header('Content-type: image/jpeg');

// Get the dimensions
list($width, $height) = getimagesize($file);

// Define our source image
$source = imagecreatefromjpeg($file);

// Creating the Canvas
$bwimage= imagecreate($width, $height);

//Creates the 256 color palette
for ($c=0;$c<256;$c++)
{
$palette[$c] = imagecolorallocate($bwimage,$c,$c,$c);
}

//Creates yiq function
function yiq($r,$g,$b)
{
return (($r*0.299)+($g*0.587)+($b*0.114));
}

//Reads the origonal colors pixel by pixel
for ($y=0;$y<$height;$y++)
{
for ($x=0;$x<$width;$x++)
{
$rgb = imagecolorat($source,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

//This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
$gs = yiq($r,$g,$b);
imagesetpixel($bwimage,$x,$y,$palette[$gs]);
}
}

// Outputs a jpg image, but you can change this to png or gif if that is what you are working with
imagejpeg($bwimage);
?>

munkynpunky
08-10-2007, 11:01 AM
http://www.ditman.net/ps/index.php

hey,

i need help actually working out how to tell the script what the image is, my other script just uses $image... but i cannot work out how to get this to work.. do you know who created that script?



<?php
/**
* This class turns a color image into Sepia
**/
class bw {

var $_bw;
var $_type;

function printImage() {
switch ($this->_type) {
case IMAGETYPE_GIF : {
header('Content-Type: image/gif');
imagegif($this->_bw);
} break;
case IMAGETYPE_JPEG : {
header('Content-Type: image/jpeg');
imagejpeg($this->_bw);
} break;
case IMAGETYPE_PNG : {
header('Content-Type: image/png');
imagepng($this->_bw);
} break;
default : {
header('Content-Type: image/jpeg');
imagejpeg($this->_bw);
} break;
} // switch
imagedestroy($this->_bw);
}

function bw($path) {
// Let's find out what type is this image:
if (@is_file($path)) {
$this->_type = @exif_imagetype($path);
switch ($this->_type) {
case IMAGETYPE_GIF : {
$img = @imagecreatefromgif($path);
} break;
case IMAGETYPE_JPEG : {
$img = @imagecreatefromjpeg($path);
} break;
case IMAGETYPE_PNG : {
$img = @imagecreatefrompng($path);
} break;
default : {
$img =_errorImg();
} break;
} // switch ($this->_type)

// If image was created successfully, do some magic stuff
if ($img) {
$this->_bw = $this->_toBW($img);
imagedestroy($img);
} // if ($this->_img)
} // if (@is_file...)
else {
$this->_bw = $this->_errorImg();
}
}
function _errorImg() {
return imagecreate(200,200);
}

function _toBW($src){
$x = imagesx($src);
$y = imagesy($src);

$dest = imagecreatetruecolor($x, $y);

for ($i=0;$i<$x;$i++) {
for ($j=0;$j<$y;$j++) {
$oldcolor = imagecolorsforindex($src, imagecolorat($src, $i, $j));
$sepiacolor = 0.299/255*$oldcolor['red'] + 0.587/255*$oldcolor['green'] + 0.114/255*$oldcolor['blue'];
$newr = (($sepiacolor + 0.1912)*255)>255?255:($sepiacolor + 0.1912)*255;
$newg = (($sepiacolor - 0.0544)*255)<0?0:($sepiacolor - 0.0544)*255;
$newb = (($sepiacolor - 0.2210)*255)<0?0:($sepiacolor - 0.2210)*255;
imagesetpixel($dest, $i, $j, imagecolorallocate($dest, $newr, $newg, $newb ));
} // for($j
} // for ($i

return $dest;
}
}

import_request_variables('g','bw_');

$bw = new bw($bw_im);
$bw->printImage();

?>

shachi
08-10-2007, 11:13 AM
No, I don't know who created that script but, what I do know is that you pass the image path e.g. :

$cm = new bw('/path/to/your/image.png');

munkynpunky
08-10-2007, 11:18 AM
ok, im lost,

so something like this?


<?php
//Get the image
$image = $_GET['image'];

$cm = new bw($image);

// Get the dimensions
list($width, $height) = getimagesize($cm);

// This class turns a color image into Sepia

class bw {

var $_bw;
var $_type;

function printImage() {
switch ($this->_type) {
case IMAGETYPE_GIF : {
header('Content-Type: image/gif');
imagegif($this->_bw);
} break;
case IMAGETYPE_JPEG : {
header('Content-Type: image/jpeg');
imagejpeg($this->_bw);
} break;
case IMAGETYPE_PNG : {
header('Content-Type: image/png');
imagepng($this->_bw);
} break;
default : {
header('Content-Type: image/jpeg');
imagejpeg($this->_bw);
} break;
} // switch
imagedestroy($this->_bw);
}

function bw($path) {
// Let's find out what type is this image:
if (@is_file($path)) {
$this->_type = @exif_imagetype($path);
switch ($this->_type) {
case IMAGETYPE_GIF : {
$img = @imagecreatefromgif($path);
} break;
case IMAGETYPE_JPEG : {
$img = @imagecreatefromjpeg($path);
} break;
case IMAGETYPE_PNG : {
$img = @imagecreatefrompng($path);
} break;
default : {
$img =_errorImg();
} break;
} // switch ($this->_type)

// If image was created successfully, do some magic stuff
if ($img) {
$this->_bw = $this->_toBW($img);
imagedestroy($img);
} // if ($this->_img)
} // if (@is_file...)
else {
$this->_bw = $this->_errorImg();
}
}
function _errorImg() {
return imagecreate($width, $height);
}

function _toBW($src){
$x = imagesx($src);
$y = imagesy($src);

$dest = imagecreatetruecolor($x, $y);

for ($i=0;$i<$x;$i++) {
for ($j=0;$j<$y;$j++) {
$oldcolor = imagecolorsforindex($src, imagecolorat($src, $i, $j));
$newcolor = (int)(($oldcolor['red'] + $oldcolor['green'] + $oldcolor['blue']) / 3);
$sepia = array(
"red" => 145,
"green" => 65,
"blue" => 10
);
$mix = array(
"o" => 0.7,
"d" => 0.3
);
$newr = ($newcolor*$mix['o'] + $sepia['red']*$mix['d'])>255?255:($newcolor*$mix['o'] + $sepia['red']*$mix['d']);
$newg = ($newcolor*$mix['o'] + $sepia['green']*$mix['d'])>255?255:($newcolor*$mix['o'] + $sepia['green']*$mix['d']);
$newb = ($newcolor*$mix['o'] + $sepia['blue']*$mix['d'])>255?255:($newcolor*$mix['o'] + $sepia['blue']*$mix['d']);
imagesetpixel($dest, $i, $j, imagecolorallocate($dest, $newr, $newg, $newb ));
} // for($j
} // for ($i

return $dest;
}
}

import_request_variables('g','bw_');

$bw = new bw($bw_im);
$bw->printImage();

?>

djr33
08-10-2007, 04:34 PM
Take a look at the filter I mentioned, IMG_FILTER_COLORIZE. That would probably be easier.

shachi
08-11-2007, 04:50 PM
True, why don't you try using image filters since they are easier than hard coding everything? However if it doesn't work out for you, you can always go back to that code and if anything goes wrong, we'll be here for you. :p