Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Trying to create a sepia image...

  1. #1
    Join Date
    Sep 2006
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to create a sepia image...

    I use this script to make my images B+W, but is there anyway to make the images appear sepia too?

    PHP Code:
    <?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 
    $bwimageimagecreate($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); 
    ?>

  2. #2
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    to make it darker, just increase RGB equally
    Decrease, surely?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, yes. That would soon be found out
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Sep 2006
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I'm using this script
    PHP Code:
    <?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($srcimagecolorat($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$jimagecolorallocate($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

  7. #7
    Join Date
    Sep 2006
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    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

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Here's something you could play with--
    PHP Code:
    <?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
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Sep 2006
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Is there anyway to edit this to get it displaying sepia?

    PHP Code:
    <?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 
    $bwimageimagecreate($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); 
    ?>

  10. #10
    Join Date
    Sep 2006
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi View Post
    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 Code:
    <?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($srcimagecolorat($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$jimagecolorallocate($dest$newr$newg$newb ));
          } 
    // for($j
        
    // for ($i
        
        
    return $dest;
      }
    }

    import_request_variables('g','bw_');

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

    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •