PHP Snippets
« 1 2 3 »image_add_border
Information
(PHP Version >= 4.0.6)Functions used in this snippet: imagesx, imagesy, str_replace, strlen, list, sscanf, imagecreatetruecolor, imagecolorallocate, imagefill, imagecopy.
Description
int image_add_border( int img, str color, int width )
Adds a width width pixel border to the image img in the color color.
Here is an Example of image_add_border
Snippet
<?php
function image_add_border($img, $color, $width=1){
$x = imagesx($img);
$y = imagesy($img);
$color = str_replace('#','',$color);
$color = strlen($color) === 3 ? $color{0} . $color{0} . $color{1} . $color{1} . $color{2} . $color{2} : $color;
list($r, $g, $b) = sscanf($color, "%2x%2x%2x");
$dest = imagecreatetruecolor(($x + ($width * 2)), ($y + ($width * 2)));
$color = imagecolorallocate($dest, $r, $g, $b);
imagefill($dest, 0, 0, $color);
imagecopy($dest, $img, $width, $width, 0, 0, $x, $y);
return $dest;
}
?>
Example
image_add_border Can be used in the following way:
<?php
$img = imagecreatefromjpeg('someimage.jpg'); // load an image
$dest = image_add_border($img, '000000');
// add a black 1 pixel border
$dest = image_add_border($img, 'FF0000', 5);
// add a red 5 pixel border
header('Content-type: image/jpeg'); // set header
imagejpeg($dest); //send to browser
?>
image_inverse
Information
(PHP Version >= 4.0.6)Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecolorat, imagesetpixel, imagecolorallocate.
Description
int image_inverse( int img )
Makes the image img have all of its colors inversed.
*Note: This is a fairly slow function so it would be best to do it once and save the file for multiple viewings.
Snippet
<?php
function image_inverse($img){
$x = imagesx($img);
$y = imagesy($img);
$dest = imagecreatetruecolor($x, $y);
for($i=0; $i<$x; $i++){
for($j=0; $j<$y; $j++){
$rgb = imagecolorat($img, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
imagesetpixel($dest, $i, $j, imagecolorallocate($dest, (255 - $r), (255 - $g), (255 - $b)));
}
}
return $dest;
}
?>
Example
image_inverse Can be used in the following way:
<?php
$img = imagecreatefromjpeg('someimage.jpg'); // create image
$img = image_inverse($img); // inverses image
header('Content-type: image/jpeg'); // send header
imagejpeg($img); // output image to browser
?>
rotate_image
Information
(PHP Version >= 3)Functions used in this snippet: imagerotate.
Description
int rotate_image( int img[, int degrees[, bool cw]] )
Uses the image img and rotates it according to degrees clockwise. If cw is set to false the function rotates the images in a counter clockwise motion.
Snippet
<?php
function rotate_image($img, $degrees='90', $cw=true){
$degrees = $cw ? $degrees : (360 - $degrees);
switch($degrees){
case '90':
$img = imagerotate($img, 90, 0);
break;
case '180':
$img = imagerotate($img, 180, 0);
break;
case '270':
$img = imagerotate($img, 270, 0);
break;
}
return $img;
}
?>
Example
rotate_image Can be used in the following way:
<?php
$img = imagecreatefromjpeg('somefile.jpg'); // load image
$img = rorate_image($img); // rotates it 90 clockwise
$img = rotate_image($img, 180); // rotates it 180 clockwise
$img = rotate_image($img, 90, false); // rotates it 90 counter clockwise
header('Content-type: image/jpeg'); // set header
imagejpeg($img); // send image to browser
?>
image_flip
Information
(PHP Version >= 4.0.6)Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecopy, imagedestroy.
Description
int image_flip( int img[, str type] )
Flips the image img to the direction of type
Snippet
<?php
function image_flip($img, $type=''){
$width = imagesx($img);
$height = imagesy($img);
$dest = imagecreatetruecolor($width, $height);
switch($type){
case '':
return $img;
break;
case 'vert':
for($i=0;$i<$height;$i++){
imagecopy($dest, $img, 0, ($height - $i - 1), 0, $i, $width, 1);
}
break;
case 'horiz':
for($i=0;$i<$width;$i++){
imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
}
break;
case 'both':
for($i=0;$i<$width;$i++){
imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
}
$buffer = imagecreatetruecolor($width, 1);
for($i=0;$i<($height/2);$i++){
imagecopy($buffer, $dest, 0, 0, 0, ($height - $i -1), $width, 1);
imagecopy($dest, $dest, 0, ($height - $i - 1), 0, $i, $width, 1);
imagecopy($dest, $buffer, 0, $i, 0, 0, $width, 1);
}
imagedestroy($buffer);
break;
}
return $dest;
}
?>
Example
image_flip Can be used in the following way:
<?php
$img = imagecreatefromjpeg('somefile.jpg'); // create an image
$img = image_flip($img, 'vert'); // flips image vertically
$img = image_flip($img, 'horiz'); // flips image horizontally
$img = image_flip($img, 'both'); // flips image both ways
header('Content-type: image/jpeg'); // send header
imagejpeg($img); // send image to browser
?>
colorallocate
Information
(PHP Version >= 3.0.6)Functions used in this snippet: str_replace, strlen, list, sscanf, imagecolorallocate.
Description
int colorallocate( str color, mixed img )
Takes the HEX color color and turns it into a color resource for the image img
Snippet
<?php
function colorallocate($color, $img){
$color = str_replace('#','',$color);
$color = strlen($color) === 3 ? $color{0} . $color{0} . $color{1} . $color{1} . $color{2} . $color{2} : $color;
list($r, $g, $b) = sscanf($color, "%2x%2x%2x");
return imagecolorallocate($img, $r, $g, $b);
}
?>
Example
colorallocate Can be used in the following way:
<?php
$img = imagecreate(50,50); // create a image
$black = colorallocate('#000000', $img); // create the color black
header('Content-type: image/jpeg'); // set image header
imagefill($img, $black); // fill the image with black
imagejpeg($img); // send the image to the browser
// another quick example
$img = imagecreate(50,50); // make an image
$red = colorallocate('F00', $img); // make the color red
imagefill($img, $red); // fill the image with red
header('Content-type: image/png'); // set header
imagepng($img); // send the image to browser
?>
« 1 2 3 »