متدهای بسیار فراوانی در این کتابخانه وجود دارد که میتوان با کمک آنها محتوای تصویری ایجاد کرد.که میتوان به کاربردهای مانند موارد زیر اشاره کرد:
چون خروجی این برنامه به صورت تصویر است بهتر است با هدر مناسب شروع کنیم
header ("Content-Type: image/jpeg");
قبل از شروع کار باید صفحه مورد نظر را ایجاد کنیم مثلا با متد ()imagecreate
$img = imagecreate (400, 400);
$blue = imagecolordeallocate($img , 0,0,255)
در مثال فوق یک رنگ ابی ایجاد کردیم
$bkg_color = imagecolorallocate($image, 255, 255, 255);
در مثال فوق یک پس زمینه سفید ایجاد کردیم
مثال:
<?php $image = imagecreate(400, 400); $bkg_color = imagecolorallocate($image, 255, 255, 255); $width = imagesx($image); $height = imagesy($image); $blue = imagecolorallocate($image, 0, 0, 255); for($i = 0; $i < 10000; $i++) { $x = rand(0, $width); $y = rand(0, $height); imagesetpixel($image, $x, $y, $blue); } header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
برنامه فوق یک عکس 400 در 200 تولید کرده و در آن ده هزار نقطه تصادفی میگذارد.
imagesetpixel($img, 0, 0, $blue);
در مثال فوق یک پیکسل ابی در نقطه 0 و 0 ایجاد کردیم
imageline($image, 0, 0, 400, 400, $blue);
یک خط که از نقطه 0و0 شروع میشود و در 400و400 تمام میشود.
imagestring($image, 5, 10, 5, "this is test", $blue);
مثال:تبدیل یک متغیر به عکس
if(isset($_GET['text'])) { $text = $_GET['text']; } if(empty($text)) { die(); } $length = strlen($text); $fw = 9; $fh = 15; $gap = 5; $width = $fw*$length + 2*$gap; $height = $fh + 2*$gap; $image = imagecreate($width, $height); $bkg_color = imagecolorallocate($image, 255, 255, 255); $blue = imagecolorallocate($image, 0, 0, 255); imagestring($image, 5, $gap, $gap, $text, $blue); header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
imagerectangle($image, 50, 100, 400, 300, $blue);
مستطیل توپر
imagefilledrectangle($image, 150, 150, 250, 250, $blue);
imageellipse($image, 400, 400, 100, 180, $red);
imageflip($image, IMG_FLIP_HORIZONTAL); imageflip($image, IMG_FLIP_VERTICAL); imageflip($image, IMG_FLIP_BOTH); $image = imagerotate($image, -30, $bkg_color);
<?php function GenerateCaptchaCode($length = 5) { $alphabet = '123456789abcdefghijklmnopqrstuvwxyz'; return SelectRandomChar($alphabet, $length); } function SelectRandomChar($str, $count = 1) { $n = strlen($str); $out = ''; for($k = 0; $k < $count; $k++) { $i = rand(0, $n - 1); $out .= substr($str, $i, 1); } return $out; } $image = imagecreate(150, 45); $bkg_color = imagecolorallocate($image, 255, 255, 255); $blue = imagecolorallocate($image, 50, 160, 255); $red = imagecolorallocate($image, 255, 80, 150); $green = imagecolorallocate($image, 200, 255, 200); imagefilledellipse($image, rand(10, 140), rand(10, 35), 30, 30, $blue); imagefilledellipse($image, rand(10, 140), rand(10, 35), 30, 30, $red); imagefilledellipse($image, rand(10, 140), rand(10, 35), 30, 30, $green); $black = imagecolorallocate($image, 0, 0, 0); $text = GenerateCaptchaCode(); $x = 20; $y = 10; for($i = 0; $i < strlen($text); $i++) { $char = substr($text, $i, 1); imagestring($image, 5, $x, $y, $char, $black); $x += rand(15, 30); $y = rand(10, 20); } header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
مثال2:
<?php session_start(); $vr = "qwertyuiopasdfghj123456789klmnbvcxz"; for($i = 1;$i <= 6; $i++){ $rcode .= $vr[rand(0,strlen($vr))]; } $_SESSION['code'] = $rcode; $img = imagecreate(180,110); $colors = array(); for($i = 0;$i <= 10; $i++){ $colors[$i] = imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255)); } $black = imagecolorallocate($img,0,0,0); for($i = 0;$i <= 5; $i++){ $xstart = rand(0,10) * 10; $xend = $xstart +10; imagefilledrectangle($img,0,$xstart,200,$xend,$colors[rand(1,9)]); } imagefttext($img,36,rand(1,30),20,90,$black,'Tahoma.ttf',$rcode); for($i = 0;$i <= 5; $i++){imageline($img,rand(0,200),rand(0,100),rand(0,200),rand(0,100),$colors[rand(1,9)]); } imagejpeg($img); ?>