Jump to content


Photo

MrFish's EasyImage Class


  • Please log in to reply
No replies to this topic

#1 groog

groog

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 05 September 2010 - 01:40 PM

Hello, you don't know me and I don't know you, but i'm a member of the W3Schools forum and I'm just spreading around my EasyImage class to the php communities. Enjoy-

EasyImage

For a project I'm working on I've needed to generate images with php. I used the GD functions without any kind of organization and realized it was going to be very hard to manage. So I've made a class called EasyImage that will manage your images a little easier and you can organize it better this way. Since it was going so well I decided to add some more functions, fill in the gaps, and share with you guys on W3Schools Pixel2Life -_-. I'm not sure if there is already something like this out there, if there is then I guess I've just wasted my time. Regardless, I will still use my own class instead. I'll provide an api of the class and a download link. It may be a little buggy, incomplete, and I still have lots of ideas for it so give my your suggestions and bug reports and I will work on them. But 90% of this version is just a repackaging of the already present functions (in an easier to use way of course). Thank you for your support!

Class EasyImage
//IMAGE
function addLayer($EasyImageObject, $x, $y, $opacity)
function antialias($boolean)
function closeImage()
function cropImage($xStart, $yStart, $xEnd, $yEnd)
function resizeCanvas($width, $height)
function resizeImage($width, $height)
function rotateImage($degrees, $hexColor)
function scaleImage($amount)
function setTile($EasyImageObject)
function setTransparentColor($hexColor)
function scaleX($amount)
function scaleY($amount)

//DRAW
function drawArc($x, $y, $width, $hheight, $start, $end, $lineThickness, $hexColor)
function drawEllipse($centerX, $centerY, $width, $height, $lineThickness, $hexColor)
function drawLine($xStart, $yStart, $xEnd, $yEnd, $lineThickness, $hexColor)
function drawCircle($centerX, $centerY, $radius, $lineThickness, $hexColor)
function drawPixel($x, $y, $hexColor)
function drawPolygon($polygonArray, $offsetX, $offsetY, $hexColor)
function drawRect($xStart, $yStart, $xEnd, $yEnd, $hexColor)
function drawString($String, $x, $y, $hexColor)

//FILL
function fill($hexColor)
function fillArc($x, $y, $width, $height, $start, $end, $hexColor)
function fillCircle($centerX, $centerY, $radius, $hexColor)
function fillEllipse($centerX, $centerY, $width, $height, $hexColor)
function fillPolygon($polygonArray, $offsetX, $offsetY, $hexColor)
function fillRect($xStart, $yStart, $xEnd, $yEnd, $hexColor)

//FILTER
function adjustBrightness($amount)
function colorize($red, $green, $blue, $alpha)
function contrast($amount)
function edgeDetect()
function emboss()
function gaussianBlur()
function grayScale()
function invert() -AKA: Negative
function meanRemoval() -AKA: Sharpen
function selectiveBlur()
function smooth($amount)

//OBJECTS
Class EasyGIF extends EasyImage
function loadImage($source)
function printImage();
function saveImage($destination)

Class EasyPNG extends EasyImage
function loadImage($source)
function printImage();
function saveImage($destination)

Class EasyJPG extends EasyImage
function loadImage($source)
function printImage();
function saveImage($destination)

I would love to go through and give a description and example image of each function. But I don't want to spend another 2 hours doing it. Instead I will give you some points that I think you may need to know, giv you a few example codes, and show you how to use this file.

Notes:

Antialias:
I've tested this function and have not gotten it to work. To my knowledge, it has to be enabled by the server first. I'm really not sure if anyone can get it working but it remains in case someone does.

Draw Functions:
For any draw function with the parameter $lineThickness, I recommend you always set it to 1 (for now). The method I use to create thicker lines didn't turn out the way I expected it to and I have a new algorithm in mind for it. I haven't gotten around to it though. You can try it, if you like the affect, use it!

Rotate Function:
This is also buggy. The $hexColor parameter is supposed to change the color of background that becomes visible as the image rotates. However, the color always shows as a dark greenish-blue for me. Maybe you will have more luck.

setTransparentColor Function:
I recommend calling this last or before you print or add a layer. If you call this function then, say, scale it 200% then you will not also scale the transparent color. So in this situation you would have to scale first, then use this function.

Transparencies:
Speaking of transparencies. If you are new to images for whatever reason, only gifs and pngs can support them. You can use it for jpgs only if the transparent area is over a opaque (fully visible), or over a gif or png image (at which point it will become part of the gif or png, no longer a jpg).

Extended Objects:
EasyGIF, EasyJPG, and EasyPNG are the only savable, loadable, and printable objects. If you create one you will still be able to use the EasyImage functions (are you new to objects?). The only use for creating an EasyImage object is if you aren't going to load, save, or print it. This can be used for layers.

Examples:

Basic Image-

<?php
	include("EasyImage.php"); 
		//We need this to start
	
	$Box = new EasyPNG(100, 100); 
		//Make a new EasyPNG object that is 100x100
		
	$Box->fill("FF00FF"); 
		//Fill the whole space with a bright purple/pink color
		
	$Box->fillRect(25, 25, 75, 75, "00FF00"); 
		//Place a green, filled rectangle at (25,25) to (75,75).
		
	$Box->drawString("A Box", 35, 50, "FF0000");
		//Write the red words, "A Box", at position (35,50).
		
	$Box->scaleImage(3); 
		//Make this 300% the original size
		
	$Box->setTransparentColor("FF00FF"); 
		//Make that original color transparent. (It's ugly)
		
	$Box->saveImage("box.png");
		//You're done- Go look at your image.
?>

Posted Image
(imageshack image)

Misc. Note: when naming objects, it's customary to capitalize every individual word. When naming variables and functions, it's customary to capitalize every word except the first.

My Fish Image-
This shows the whole purpose of PHP images. PHP GD isn't just-another-photoshop. It's much less powerful, slower, and harder to use in comparison, but there is one thing photoshop can't do- you can't create dynamic text inside (Number of downloads):

<?php
		$Background = new EasyPNG(300, 150);
	$Background->fill("669999");
	$Background->drawRect(0, 0, 299, 149, "33404D");
	
	/*FISH*/
	
	$fishColor = "75BAFF";
	$transColor = "FF00FF";
	$Fish = new EasyPNG(300, 150);
	//Make the background transparent (default is black)
	$Fish->fill("FF00FF");
	
	//Head
	$Fish->fillArc(150, 75, 75, 75, 30, 310, $fishColor);
	$Fish->fillCircle(140, 65, 10, "FFFFFF");
	$Fish->fillCircle(140, 65, 5, "000000");
	//body
	$Fish->fillRect(45, 50, 130, 100, $fishColor);
	//Tail
	$Fish->fillCircle(40, 75, 35, $fishColor);
	$Fish->fillCircle(30, 75, 35, $transColor);
	
	//Legs (tiktaalik ftw)
	$Fish->drawLine(75, 100, 75, 120, 5, $fishColor);
		$Fish->drawLine(75, 120, 85, 120, 5, $fishColor);
	$Fish->drawLine(105, 100, 105, 120, 5, $fishColor);	
		$Fish->drawLine(105, 120, 115, 120, 5, $fishColor);
		
	/*TEXT*/
	
	$Text =	new EasyImage(300, 150);
	$Text->fill($transColor);
	$Text->drawString("Generated with EasyImage |", 5, 130, "000000");	
	$Text->drawString(" Downloads: " . $downloads, 190, 130, "990000");
	$Text->drawString("that's", 170, 30, "000000");
	$Text->font = 5;
		   //I don't have a setFontSize() function. You can do it manually like this. Sizes range from 1-5 (default is 3)
	$Text->drawString("MISTER", 180, 50, "000000");
	$Text->font = 3;
	$Text->drawString("fish", 190, 70, "000000");
	$Text->drawString("to you, buddy! D:<", 170, 90, "000000");
		
	//put it all together
	$Fish->setTransparentColor($transColor);
	$Text->setTransparentColor($transColor);
	$Background->addLayer($Fish, -20, -10, 100);	
	$Background->addLayer($Text, 0, 0, 100);
	
	$Background->printImage();
	$Background->saveImage("fishimage.png");
?>

Posted Image
(imageshack image)

With this you can create things like thumbnails, click/visit/download counters, captchas, game server info, etc.

Download: http://mwilliamson98...eteasyimage.php

Comments always appreciated. I know this sort of thing probably isn't in high demand but hopefully this doesn't turn into a ghost topic ><

ENJOY




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users