PHP Snippets
1 2 3 »imagecopyresamplebicubic
Information
(PHP Version >= 4.0.1)Functions used in this snippet: imagepalettecopy, round, imagecolorsforindex, imagecolorat, imagesetpixel, imagecolorclosest.
Description
int imagecopyresamplebicubic( int &dst_img, int &src_img, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
This function will resize images using a Bicubic method. This makes the resized images look fabulous, but as a result is extremely slow. It takes the same arguements as imagecopyresized.
I must note that I did NOT write this function. I found it on php.net at http://www.php.net/manual/en/function.imagecopyresampled.php#44490
Snippet
<?php
function imagecopyresamplebicubic(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
imagepalettecopy($dst_img, $src_img);
$rX = $src_w / $dst_w;
$rY = $src_h / $dst_h;
$w = 0;
for($y = $dst_y; $y < $dst_h; $y++)
{
$ow = $w; $w = round(($y + 1) * $rY);
$t = 0;
for($x = $dst_x; $x < $dst_w; $x++)
{
$r = $g = $b = 0; $a = 0;
$ot = $t; $t = round(($x + 1) * $rX);
for($u = 0; $u < ($w - $ow); $u++)
{
for($p = 0; $p < ($t - $ot); $p++)
{
$c = imagecolorsforindex($src_img, imagecolorat($src_img, $ot + $p, $ow + $u));
$r += $c['red'];
$g += $c['green'];
$b += $c['blue'];
$a++;
}
}
imagesetpixel($dst_img, $x, $y, imagecolorclosest($dst_img, $r / $a, $g / $a, $b / $a));
}
}
}
?>
Example
imagecopyresamplebicubic Can be used in the following way:
<?php
$img = imagecreatefrompng('someimage'); // image is 750x750
$dest = imagecreatetruecolor(100, 100); // create an empty image
imagecopyresamplebicubic($dest, $img, 0, 0, 0, 0, 100, 100, 750, 750);
header('Content-type: image/png');
imagepng($dest);
?>
image_watermark
Information
(PHP Version >= 3)Functions used in this snippet: imagecreatefromjpeg, imagesx, imagesy, imagecreatefrompng, imagecreatetruecolor, imagealphablending, imagecopy, min.
Description
resource image_watermark( resource img, resource wmg )
Creates a watermark layover image over an image. The watermark image will be repeated on both the x and y axis. Using an image that will work as a seamless repeating image is best.
If the watermark is in PNG format and has its alpha transparceny set, the final watermark layover will be the same opacity as the watermark image.
Snippet
<?php
function image_watermark($img, $wmg)
{
$ix = imagesx($img);
$iy = imagesy($img);
$wx = imagesx($wmg);
$wy = imagesy($wmg);
$fmg = imagecreatetruecolor($ix, $iy);
imagealphablending($fmg, true);
imagecopy($fmg, $img, 0, 0, 0, 0, $ix, $iy);
for($sx = 0; $sx < $ix; $sx += $wx)
{
for($sy = 0; $sy < $iy; $sy += $wy)
{
imagecopy($fmg, $wmg, $sx, $sy, 0, 0, min($ix - $sx, $wx), min($iy - $sy, $wy));
}
}
return $fmg;
}
?>
Example
image_watermark Can be used in the following way:
<?php
// make our image into a php resource
$img = imagecreatefromjpeg('yourimage.jpg');
// make our watermark into a php resource
$wmg = imagecreatefrompng('your_watermark.png');
// apply the watermark
$fmg = image_watermark($img, $wmg);
// send the image to the browser
imagejpeg($fmg);
?>
image_constraints
Information
(PHP Version >= 4.0.6)Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecopyresized.
Description
int image_constraints( int img, int dimx, int dimy )
Creates a resized image with the maximum width being dimx and height dimy without distorting the image.
Snippet
<?php
function image_constraints($img, $dimx, $dimy){
$x = imagesx($img);
$y = imagesy($img);
$xorg = $x;
$yorg = $y;
$altered = false;
if($x > $dimx){
$ratio = ($dimx / $x);
$x = $ratio * $x;
$y = $ratio * $y;
$altered = true;
}
if($y > $dimy){
$ratio = ($dimy / $y);
$y = $ratio * $y;
$x = $ratio * $x;
$altered = true;
}
if(!$altered){
return $img;
}
$dest = imagecreatetruecolor($x, $y);
imagecopyresized($dest, $img, 0, 0, 0, 0, $x, $y, $xorg, $yorg);
return $dest;
}
?>
Example
image_constraints Can be used in the following way:
<?php
$img = imagecreatefromjpeg('someimage.jpg'); // img size is 250 by 300 we will say
$img = image_constraints($img, 100, 100);
imagejpeg($img); // output image is 83 by 100
?>
image_borders
Information
(PHP Version >= 4.1.6)Functions used in this snippet: is_array, strtolower, substr, strrpos, imagesx, imagesy, imagecreate, imagecopy, min.
Description
int image_borders( int img, array borders )
Adds borders to the image img. The borders array should have the indexes tl, tt, tr, rr, br, bb, bl, ll correspoding to that area's border. (see the example below for a better explanation)
Note: This function is fairly slow, so it would be best to apply it once, then save it for future showings.
Also, the wider the top(tt) and bottom(bb) and the longer the left(ll) and right(rr) borders are the faster the function will perform. They CAN be longer and wider than the actual image itself.
Snippet
<?php
function image_borders($img, $borders){
if(!is_array($borders)){
return $img;
}
$border_data = array();
$types = array('jpeg'=>'imagecreatefromjpeg', 'jpg'=>'imagecreatefromjpeg', 'gif'=>'imagecreatefromgif', 'png'=>'imagecreatefrompng');
foreach($borders as $key=>$value){
$border_data[$key]['img'] = $types[strtolower(substr($value, strrpos($value, '.') + 1))]($value);
$border_data[$key]['x'] = imagesx($border_data[$key]['img']);
$border_data[$key]['y'] = imagesy($border_data[$key]['img']);
}
$x = imagesx($img);
$y = imagesy($img);
$dest = imagecreate(($x + $border_data['ll']['x'] + $border_data['rr']['x']), ($y + $border_data['tt']['y'] + $border_data['bb']['y']));
$max_x = $x + $border_data['ll']['x'];
$actual_x = $border_data['ll']['x'];
$ttx = $border_data['ll']['x'];
$bbx = $border_data['ll']['x'];
while($actual_x < $max_x){
if($ttx < $max_x){
imagecopy($dest, $border_data['tt']['img'], $ttx, 0, 0, 0, $border_data['tt']['x'], $border_data['tt']['y']);
$ttx += $border_data['tt']['x'];
}
if($bbx < $max_x){
imagecopy($dest, $border_data['bb']['img'], $bbx, ($border_data['ll']['y'] + $y), 0, 0, $border_data['bb']['x'], $border_data['bb']['y']);
$bbx += $border_data['bb']['x'];
}
$actual_x = min($ttx, $bbx);
}
$max_y = $y + $border_data['tt']['y'];
$actual_y = $border_data['tt']['y'];
$lly = $border_data['tt']['y'];
$rry = $border_data['rr']['y'];
while($actual_y < $max_y){
if($lly < $max_y){
imagecopy($dest, $border_data['ll']['img'], 0, $lly, 0, 0, $border_data['ll']['x'], $border_data['ll']['y']);
$lly += $border_data['ll']['y'];
}
if($rry < $max_y){
imagecopy($dest, $border_data['rr']['img'], ($border_data['ll']['x'] + $x), $rry, 0,0, $border_data['rr']['x'], $border_data['rr']['y']);
$rry += $border_data['rr']['y'];
}
$actual_y = min($lly, $rry);
}
imagecopy($dest, $border_data['tl']['img'], 0, 0, 0, 0, $border_data['tl']['x'], $border_data['tl']['y']);
imagecopy($dest, $border_data['tr']['img'], ($x + $border_data['tl']['x']), 0, 0, 0, $border_data['tr']['x'], $border_data['tr']['y']);
imagecopy($dest, $border_data['bl']['img'], 0, ($y + $border_data['tl']['y']), 0, 0, $border_data['bl']['x'], $border_data['bl']['y']);
imagecopy($dest, $border_data['br']['img'], ($x + $border_data['tl']['x']), ($y + $border_data['tl']['y']), 0, 0, $border_data['br']['x'], $border_data['br']['y']);
imagecopy($dest, $img, $border_data['ll']['x'], $border_data['tt']['y'], 0, 0, $x, $y);
return $dest;
}
?>
Example
image_borders Can be used in the following way:
<?php
$img = imagecreatefromjpeg('baseimage.jpg');
// create the base image
$borders = array( 'tl'=>'topleft.jpg',
'tt'=>'top.jpg',
'tr'=>'topright.jpg',
'rr'=>'right.jpg',
'br'=>'bottomright.jpg',
'bb'=>'bottom.jpg',
'bl'=>'bottomleft.jpg',
'll'=>'left.jpg');
// create your array with borders
// Note, the array indexes must be the same as above
$dest = image_borders($img, $borders);
// actually make the image
header('Content-type: image/png');
// send a header
imagepng($dest);
// send it to the browser
?>
image_contrast
Information
(PHP Version >= 4.0.6)Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecolorat, min, floor, max, ceil, imagesetpixel, imagecolorallocate.
Description
int image_contrast( int img, int percent )
Adjusts the contrast of the image img based on the percent of percent. Percent is any number above 0. The lower the number, the darker the image will get. The higher the number, the brighter the image will get.
*Note: This function is fairly slow, so it would be best to apply the function once and save the image for further viewings.
You can view an example of this at Example of image_contrast
Snippet
<?php
function image_contrast($img, $percent){
$x = imagesx($img);
$y = imagesy($img);
$dest = imagecreatetruecolor($x, $y);
$percent = $percent / 100;
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;
$r = $percent > 1 ? min(floor($r * $percent), 255) : max(ceil($r * $percent), 0);
$g = $percent > 1 ? min(floor($g * $percent), 255) : max(ceil($g * $percent), 0);
$b = $percent > 1 ? min(floor($b * $percent), 255) : max(ceil($b * $percent), 0);
imagesetpixel($dest, $i, $j, imagecolorallocate($dest, $r, $g, $b));
}
}
return $dest;
}
?>
Example
image_contrast Can be used in the following way:
<?php
$img = imagecreatefromjpeg('someimage.jpg'); // load an image
$dest = image_contrast($img, 50);
// decrease contrast by 50%
$dest = image_contrast($img, 150);
// increase contrast by 50%
header('Content-type: image/jpeg'); // set header
imagejpeg($dest); // send to browser
?>
1 2 3 »