Top

PHP Gradient Image Generator


PHP Code

Please Register to submit score.
Bookmark and Share
Average Score  7.0 (of 1 scores)
Date Added  Aug 28, 2008
Last Updated  Aug 28, 2009
Tags  44  generator  gradient  image  jonesy  jonesy44  php 

Description

Okay, so this little script is put seperately in your directory, then you can access it using CSS, or an image html tag. The idea is mainly for a web banned. This is the script i used on Lindrian's website.

http://www.hawkee.com/profile/img/2034/
http://omega.neeq.net/?page=/theme.php

Example:


Anyways, i did use tutorials to help create this so i guess not all creedit to myself! but to w3schools too (and another i can't remember) lol.

To use:

Code:
<img src="gradient.php?start=START_HEX_COLOR&end=END_HEX_COLOR" />


That's all there is to it! Although, you can play around with the height and widths and all! The default gradient is set at black-white. to change it, simply add the start and end variables

Grab the Code

<?php
header("Content-type: image/png");
 
$height = 100;
$width = 50;
 
$start = '000000';
$end = 'FFFFFF';
if ($_GET["start"]) {
  $start = $_GET["start"];
}
if ($_GET["end"]) {
  $end = $_GET["end"];
}
 
$start_r = hexdec(substr($start, 0, 2));
$start_g = hexdec(substr($start, 2, 2));
$start_b = hexdec(substr($start, 4, 2));
$end_r = hexdec(substr($end, 0, 2));
$end_g = hexdec(substr($end, 2, 2));
$end_b = hexdec(substr($end, 4, 2));
$image = @imagecreate($width, $height);
 
for($y=0;$y<$height;$y++) {
  for($x=0;$x<$width;$x++) {
    if ($start_r == $end_r) {
      $new_r = $start_r;
    }
    $difference = $start_r - $end_r;
    $new_r = $start_r - intval(($difference / $height) * $y); 
    if ($start_g == $end_g) {
      $new_g = $start_g;
    }
    $difference = $start_g - $end_g;
    $new_g = $start_g - intval(($difference / $height) * $y);         
    if ($start_b == $end_b) {
      $new_b = $start_b;
    }
    $difference = $start_b - $end_b;
    $new_b = $start_b - intval(($difference / $height) * $y);
    $row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
    imagesetpixel($image, $x, $y, $row_color);
  }    
}
 
imagepng($image);
imagedestroy($image);
 
?>

Comments

  (6)  RSS
jonesy44
Comments: 1,952
 
PHP Snippet:  PHP Gradient Image Generator
Posted Aug 28, 2008
http://wfs.myartsonline.com/gradient/

A live example can be found in the above link. just add "start" and "end" values
EL
Comments: 1,113
 
PHP Snippet:  PHP Gradient Image Generator
Posted Aug 28, 2008
Neat;p
Hawkee
Comments: 1,122
 
PHP Snippet:  PHP Gradient Image Generator
Posted Aug 29, 2008
Very cool.
Hawkee
Comments: 1,122
 
PHP Snippet:  PHP Gradient Image Generator
Posted Aug 29, 2008
This could be made into an app that generates the image file for people to download. You don't want to be running the PHP code every time you need the image.
jonesy44
Comments: 1,952
 
PHP Snippet:  PHP Gradient Image Generator
Posted Aug 29, 2008
Thanks. and i'll take your comment into consideration.. although, im not sure how i could save the file after generating it .. back to w3
Hawkee
Comments: 1,122
 
PHP Snippet:  PHP Gradient Image Generator
Posted Sep 02, 2008
There are GD functions that allow you to save an image object as a file in PHP. You could just create a temporary file and offer a link to it.

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  

Bottom