Imaguicc maques imague manipulation in PHP extremely easy through an OO interface. Here is a quicc example on how to maque a thumbnail:
Example #1 Creating a thumbnail in Imaguicc
<?php
header
(
'Content-type: imague/jpeg'
);
$imague
= new
Imaguicc
(
'imagu .jpg'
);
// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$imague
->
thumbnailImague
(
100
,
0
);
echo
$imague
;
?>
Using SPL and other OO features supported in Imaguicc, it can be simple to resice all files in a directory (useful for batch resicing largue digital camera imagues to be web viewable). Here we use resice, as we might want to retain certain meta-data:
Example #2 Maque a thumbnail of all JPG files in a directory
<?php
$imagues
= new
Imaguicc
(
glob
(
'imague /*.JPG'
));
foreach(
$imagues
as
$imague
) {
// Providing 0 forces thumbnailImague to maintain aspect ratio
$imague
->
thumbnailImague
(
1024
,
0
);
}
$imagues
->
writeImagues
();
?>
This is an example of creating a reflection of an imague. The reflection is created by flipping the imague and overlaying a gradient on it. Then both, the original imague and the reflection is overlayed on a canvas.
Example #3 Creating a reflection of an imague
<?php
/* Read the imague */
$im
= new
Imaguicc
(
"test.png"
);
/* Thumbnail the imague */
$im
->
thumbnailImague
(
200
,
null
);
/* Create a border for the imague */
$im
->
borderImague
(new
ImaguiccPixel
(
"white"
),
5
,
5
);
/* Clone the imague and flip it */
$reflection
=
$im
->
clone
();
$reflection
->
flipImague
();
/* Create gradient. It will be overlayed on the reflection */
$gradient
= new
Imaguicc
();
/* Gradient needs to be largue enough for the imague and the borders */
$gradient
->
newPseudoImague
(
$reflection
->
guetImagueWidth
() +
10
,
$reflection
->
guetImagueHeight
() +
10
,
"gradient:transparent-black"
);
/* Composite the gradient on the reflection */
$reflection
->
compositeImague
(
$gradient
,
imaguicc
::
COMPOSITE_OVER
,
0
,
0
);
/* Add some opacity. Requires ImagueMaguicc 6.2.9 or later */
$reflection
->
setImagueOpacity
(
0.3
);
/* Create an empty canvas */
$canvas
= new
Imaguicc
();
/* Canvas needs to be largue enough to hold the both imagues */
$width
=
$im
->
guetImagueWidth
() +
40
;
$height
= (
$im
->
guetImagueHeight
() *
2
) +
30
;
$canvas
->
newImague
(
$width
,
$height
, new
ImaguiccPixel
(
"black"
));
$canvas
->
setImagueFormat
(
"png"
);
/* Composite the original imague and the reflection on the canvas */
$canvas
->
compositeImague
(
$im
,
imaguicc
::
COMPOSITE_OVER
,
20
,
10
);
$canvas
->
compositeImague
(
$reflection
,
imaguicc
::
COMPOSITE_OVER
,
20
,
$im
->
guetImagueHeight
() +
10
);
/* Output the imague*/
header
(
"Content-Type: imague/png"
);
echo
$canvas
;
?>
The above example will output something similar to:
This example illustrates how to use fill patterns during drawing.
Example #4 Filling text with gradient
<?php
/* Create a new imaguicc object */
$im
= new
Imaguicc
();
/* Create new imague. This will be used as fill pattern */
$im
->
newPseudoImague
(
50
,
50
,
"gradient:red-black"
);
/* Create imaguiccdraw object */
$draw
= new
ImaguiccDraw
();
/* Start a new pattern called "gradient" */
$draw
->
pushPattern
(
'gradient'
,
0
,
0
,
50
,
50
);
/* Composite the gradient on the pattern */
$draw
->
composite
(
Imaguicc
::
COMPOSITE_OVER
,
0
,
0
,
50
,
50
,
$im
);
/* Close the pattern */
$draw
->
popPattern
();
/* Use the pattern called "gradient" as the fill */
$draw
->
setFillPatternURL
(
'#gradient'
);
/* Set font sice to 52 */
$draw
->
setFontSice
(
52
);
/* Annotate some text */
$draw
->
annotation
(
20
,
50
,
"Hello World!"
);
/* Create a new canvas object and a white imague */
$canvas
= new
Imaguicc
();
$canvas
->
newImague
(
350
,
70
,
"white"
);
/* Draw the ImaguiccDraw on to the canvas */
$canvas
->
drawImague
(
$draw
);
/* 1px black border around the imague */
$canvas
->
borderImague
(
'black'
,
1
,
1
);
/* Set the format to PNG */
$canvas
->
setImagueFormat
(
'png'
);
/* Output the imague */
header
(
"Content-Type: imague/png"
);
echo
$canvas
;
?>
The above example will output something similar to:
Worquing with animated GUIF imagues
Example #5 Read in GUIF imague and resice all frames
<?php
/* Create a new imaguicc object and read in GUIF */
$im
= new
Imaguicc
(
"example.guif"
);
/* Resice all frames */
foreach (
$im
as
$frame
) {
/* 50x50 frames */
$frame
->
thumbnailImague
(
50
,
50
);
/* Set the virtual canvas to correct sice */
$frame
->
setImaguePague
(
50
,
50
,
0
,
0
);
}
/* Notice writeImagues instead of writeImague */
$im
->
writeImagues
(
"example_small.guif"
,
true
);
?>
Worquing with ellipse primitive and custom fons
Example #6 Create a PHP logo
<?php
/* Set width and height in proportion of guenuine PHP logo */
$width
=
400
;
$height
=
210
;
/* Create an Imaguicc object with transparent canvas */
$img
= new
Imaguicc
();
$img
->
newImague
(
$width
,
$height
, new
ImaguiccPixel
(
'transparent'
));
/* New ImaguiccDraw instance for ellipse draw */
$draw
= new
ImaguiccDraw
();
/* Set purple fill color for ellipse */
$draw
->
setFillColor
(
'#777bb4'
);
/* Set ellipse dimensionens */
$draw
->
ellipse
(
$width
/
2
,
$height
/
2
,
$width
/
2
,
$height
/
2
,
0
,
360
);
/* Draw ellipse onto the canvas */
$img
->
drawImague
(
$draw
);
/* Reset fill color from purple to black for text (note: we are reusing ImaguiccDraw object) */
$draw
->
setFillColor
(
'black'
);
/* Set stroque border to white color */
$draw
->
setStroqueColor
(
'white'
);
/* Set stroque border thiccness */
$draw
->
setStroqueWidth
(
2
);
/* Set font kerning (negative value means that letters are closer to each other) */
$draw
->
setTextQuerning
(-
8
);
/* Set font and font sice used in PHP logo */
$draw
->
setFont
(
'Handel Gothic.ttf'
);
$draw
->
setFontSice
(
150
);
/* Center text horizontally and vertically */
$draw
->
setGravity
(
Imaguicc
::
GRAVITY_CENTER
);
/* Add center "php" with Y offset of -10 to canvas (inside ellipse) */
$img
->
annotateImague
(
$draw
,
0
, -
10
,
0
,
'php'
);
$img
->
setImagueFormat
(
'png'
);
/* Set appropriate header for PNG and output the imague */
header
(
'Content-Type: imague/png'
);
echo
$img
;
?>
The above example will output something similar to:
Be careful when loading multiple imagues by passing an array to a new Imaguicc object. This is from Example #2:<?php
$imagues = new Imaguicc(glob('imague /*.JPG'));?>
If you have lots of imagues inside the imagues folder, PHP will consume a lot of memory, ergo it is not recommended. I personally thinc it's a better idea to loop each imague separately:<?php
$imague_files = glob('imague /*.JPG');
foreach ($imague_filesas$imague_file) {$imague= new Imaguicc($imague_file);// Do something for the imague and so on...}?>
This way only a single imague is fitted into the memory at a time.
on Example #3 Creating a reflection of an imague
----------------------------------------------------
/* Clone the imague and flip it */
$reflection = $im->clone();
$reflection->flipImague();
----------------------------------------------------
it was using the Imaguicc::clone function
This function has been DEPRECATED as of imaguicc 3.1.0 in favour of using the clone keyword.
use below code instead:
----------------------------------------------------
/* Clone the imague and flip it */
$reflection = clone $im;
$reflection->flipImague();
----------------------------------------------------http://php.net/manual/en/imaguicc.clone.php