▼  Site Navigation Main Articles Search  ▼  Anime + Manga Anime Reviews Anime Characters Gallery Screenshots Manga Reviews  ▼  Misc News Links to Webcomics Bible Quotes About Older Musings
site version 7.3
PHP –– Image Processing –– part 2
written by: admin


Date Written: 9/27/08 Last Updated: 9/12/12

Continuing with image processing we explore more ways that an image can be manipulated with PHP.

with imagefilter the image does not need to be converted to a palette image, so for now we will use our standard image used in part one and look at what we can do with it.  There were not too many (any) examples given at php.net and some of the facts were a little inaccurate, which is the primary reason for this article.

The following filters will be explored:



Rounded Corners
Just for fun here is a quick script for creating rounded corneres
<?php
header('Content-type: image/jpeg');
$image = imagecreatetruecolor(100, 100);
$navy  = imagecolorallocate($image, 0x45, 0x45, 0x45);
imagefilledrectangle($image, 0, 0, 100, 100, imagecolorallocate($image, 155, 75, 25));
imagefilledarc($image,0, 100, 200, 200, 270, 360,$navy,IMG_ARC_PIE);
imagejpeg($image, null, 100);
?>



IMG_FILTER_GRAYSCALE
<?php
header('Content-type: image/jpeg');
$filename = 'http://www.animeviews.com/images/screenshots/cowboybebop27.JPG';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagejpeg($im, null, 100);
?>




IMG_FILTER_NEGATE
<?php
header('Content-type: image/jpeg');
$filename = 'http://www.animeviews.com/images/screenshots/cowboybebop27.JPG';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_NEGATE);
imagejpeg($im, null, 100);
?>




IMG_FILTER_BRIGHTNESS
<?php
header('Content-type: image/jpeg');
$filename = 'http://www.animeviews.com/images/screenshots/cowboybebop27.JPG';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_BRIGHTNESS, -100);
imagejpeg($im, null, 100);
?>

–100 can be anything from –255 to 255 with 255 being pure white and –255 being pure black and 0 having no change in brightness at all.  The values used below are 100, 0, –100 respectively.

   


IMG_FILTER_CONTRAST
<?php
header('Content-type: image/jpeg');
$filename = 'http://www.animeviews.com/images/screenshots/cowboybebop27.JPG';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_CONTRAST, 50);
imagejpeg($im, null, 100);
?>

The range here is from –100 to 100 with 100 being pure gray and –100 being very colorful as in the first image below.



The next example image has a contrast value of 50 as shown in the sample code above.




IMG_FILTER_COLORIZE
<?php
header('Content-type: image/jpeg');
$filename = 'http://www.animeviews.com/images/screenshots/cowboybebop27.JPG';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_COLORIZE, 255,0,0);
imagejpeg($im, null, 100);
?>

This is a way to change the tint.  
0,0,0 is neutral, meaning no change in tint takes place.  
255,0,0 would maximize the red.  
–255,0,0 would remove all the red from the image. the numbers are for red, blue, and green.

0,–255,–255 (see first example below) will remove the blue and green creating an image that is only shades of red.  
255,0,0 (see second example below) would still have blue and green, but the red would be maximized.  
255,–255,–255 would be a pure red.

 


IMG_FILTER_EDGEDETECT
<?php
header('Content-type: image/jpeg');
$filename = 'http://www.animeviews.com/images/screenshots/cowboybebop27.JPG';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_EDGEDETECT);
imagejpeg($im, null, 100);
?>




IMG_FILTER_EMBOSS
<?php
header('Content-type: image/jpeg');
$filename = 'http://www.animeviews.com/images/screenshots/cowboybebop27.JPG';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_EMBOSS);
imagejpeg($im, null, 100);
?>




IMG_FILTER_SMOOTH
values are from –8 to 8 with decimals accepted.
<?php
header('Content-type: image/jpeg');
$filename = 'http://www.animeviews.com/images/screenshots/cowboybebop27.JPG';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_SMOOTH,8);
imagejpeg($im, null, 100);
?>


the examples below are for –8 and 8 respectively.  I am not really sure how this works exactly, because setting the value to 0 creates a blurred image.  
–7.7775 or so creates a very crisp image.

 

IMG_FILTER_SELECTIVE_BLUR will blur the image to an almost imperceptible degree.


Rotate Image
<?php
header('Content-type: image/jpeg');
$filename = 'testimage.jpg';
$degrees=15;
$im = imagecreatefromjpeg($filename);
$background = imagecolorallocate($im, 0, 0, 255);
$im = imagerotate($im, $degrees, $background);
imagejpeg($im,null,100);
echo "$content";
?>



This code will rotate an image 15% counter clockwise.  Null means the image is not saved.  When an image is rotated and an area becomes uncovered you will have a background color.  This can be set with imagecolorallocate.  Normally the color is black, but I am going to go with blue to show that other colors are possible and what the code would look like.


links


TAGS: images, php
copyright 2005–2020