public function Resize($image, $newWidth, $targetName) { if (!file_exists(PUBLIC_ROOT . $image)) { $image = '/assets/images/not-found.gif'; } $imgInfo = getimagesize(PUBLIC_ROOT . $image); $oldWidth = $imgInfo[0]; $oldHeight = $imgInfo[1]; $changeRatio = $oldWidth / $newWidth; $newHeight = round($oldHeight / $changeRatio); $newImage = imagecreatetruecolor($newWidth, $newHeight); $source = $this->load(PUBLIC_ROOT . $image); if ($this->imageType == IMAGETYPE_PNG) { imagealphablending($newImage, false); imagesavealpha($newImage, true); $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127); imagefilledrectangle($newImage, 0, 0, $newWidth, $newHeight, $transparent); } imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight); header('Content-Type: image/jpeg'); imagejpeg($newImage, $targetName, 100); Debugger::debug($targetName, 'TARGET'); //$this->save($targetName); $this->image = $newImage; imagedestroy($newImage); }
/** * Generate a gradient image * * @param int $width The new image width in pixels * @param int $height The new image height in pixels * @param int $start The start of the gradient in pixels * @param Color $src_color The start image color * @param Color $dest_color The end image color * * @return \imagemanipulation\ImageResource */ public static function gradient($width, $height, $start, Color $src_color, Color $dest_color) { Args::int($width, 'width')->required()->min(1); Args::int($height, 'height')->required()->min(1); $res = self::create($width, $height, $src_color); $img = $res->getResource(); $srcA = $src_color->getAlpha(); $srcR = $src_color->getRed(); $srcG = $src_color->getGreen(); $srcB = $src_color->getBlue(); $destA = $dest_color->getAlpha(); $destR = $dest_color->getRed(); $destG = $dest_color->getGreen(); $destB = $dest_color->getBlue(); $incA = ($destA - $srcA) / ($width - $start); $incR = ($destR - $srcR) / ($width - $start); $incG = ($destG - $srcG) / ($width - $start); $incB = ($destB - $srcB) / ($width - $start); for ($i = $start; $i < $width; $i++) { $srcA += $incA; $srcB += $incB; $srcG += $incG; $srcR += $incR; imagefilledrectangle($img, $i, 0, $i, $height, imagecolorallocatealpha($img, $srcR, $srcG, $srcB, $srcA)); } return $res; }
public function index() { header("content-type:image/gif"); $this->load->library('session'); $width = 60; $height = 30; $img = imagecreatetruecolor($width, $height); $white = imagecolorallocate($img, 255, 255, 255); $color = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255)); $str = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $code = ""; for ($i = 0; $i < 5; $i++) { $code .= $str[rand(0, strlen($str) - 1)]; } $this->session->set_userdata(array('myCode' => strtolower($code))); imagefilledrectangle($img, 0, 0, 60, 30, $color); for ($i = 0; $i < 100; $i++) { imagesetpixel($img, rand(0, 60), rand(0, 30), imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255))); } for ($i = 0; $i < 10; $i++) { imageline($img, rand(0, 60), rand(0, 30), rand(0, 60), rand(0, 30), imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255))); } imagestring($img, 5, 10, 5, $code, $white); return imagegif($img); //imagedestroy($img); }
function resize($photo_src, $width, $name) { $parametr = getimagesize($photo_src); list($width_orig, $height_orig) = getimagesize($photo_src); $ratio_orig = $width_orig / $height_orig; $new_width = $width; $new_height = $width / $ratio_orig; $newpic = imagecreatetruecolor($new_width, $new_height); $col2 = imagecolorallocate($newpic, 255, 255, 255); imagefilledrectangle($newpic, 0, 0, $new_width, $new_width, $col2); switch ($parametr[2]) { case 1: $image = imagecreatefromgif($photo_src); break; case 2: $image = imagecreatefromjpeg($photo_src); break; case 3: $image = imagecreatefrompng($photo_src); break; } imagecopyresampled($newpic, $image, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); imagejpeg($newpic, $name, 100); return true; }
function imgcode($nums, $width, $high) { //去除了數字0和1 字母小寫O和L,為了避免辨識不清楚 //$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMOPQRSTUBWXYZ"; $str = "0123456789"; $code = ''; for ($i = 0; $i < $nums; $i++) { $code .= $str[mt_rand(0, strlen($str) - 1)]; } $_SESSION['captcha'] = $code; //建立圖示,設置寬度及高度與顏色等等條件 $image = imagecreate($width, $high); $black = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200)); $border_color = imagecolorallocate($image, 21, 106, 235); $background_color = imagecolorallocate($image, 235, 236, 237); //建立圖示背景 imagefilledrectangle($image, 0, 0, $width, $high, $background_color); //建立圖示邊框 imagerectangle($image, 0, 0, $width - 1, $high - 1, $border_color); //在圖示布上隨機產生大量躁點 for ($i = 0; $i < 80; $i++) { imagesetpixel($image, rand(0, $width), rand(0, $high), $black); } $strx = rand(3, 8); for ($i = 0; $i < $nums; $i++) { $strpos = rand(1, 6); imagestring($image, 5, $strx, $strpos, substr($code, $i, 1), $black); $strx += rand(10, 30); } imagepng($image); imagedestroy($image); }
/** * Create thumbnail image by php using the GD Library * * @since 1.0.0 */ public static function createThumb($source_image, $destination_image_url, $width, $height, $quality, $crop = null, $prefix = null, $checksize = null) { //Set image ratio list($w, $h) = getimagesize($source_image); // resize if ($crop === true) { if ($checksize && ($w < $width or $h < $height)) { $width = $w + 1; $height = $h + 1; $x = 0; } else { $ratio = max($width / $w, $height / $h); $h = $height / $ratio; $x = ($w - $width / $ratio) / 2; $w = $width / $ratio; } } else { if ($checksize && ($w < $width or $h < $height)) { $width = $w; $height = $h; $x = 0; } else { $ratio = min($width / $w, $height / $h); $width = $w * $ratio; $height = $h * $ratio; $x = 0; } } if (preg_match("/.jpg/i", "{$source_image}") or preg_match("/.jpeg/i", "{$source_image}")) { //JPEG type thumbnail $destImage = imagecreatetruecolor($width, $height); $sourceImage = imagecreatefromjpeg($source_image); imagecopyresampled($destImage, $sourceImage, 0, 0, $x, 0, $width, $height, $w, $h); imagejpeg($destImage, $destination_image_url, $quality); imagedestroy($destImage); } elseif (preg_match("/.png/i", "{$source_image}")) { //PNG type thumbnail $destImage = imagecreatetruecolor($width, $height); $sourceImage = imagecreatefrompng($source_image); imagealphablending($destImage, false); imagecopyresampled($destImage, $sourceImage, 0, 0, $x, 0, $width, $height, $w, $h); imagesavealpha($destImage, true); imagepng($destImage, $destination_image_url); } elseif (preg_match("/.gif/i", "{$source_image}")) { //GIF type thumbnail $destImage = imagecreatetruecolor($width, $height); $sourceImage = imagecreatefromgif($source_image); $bgc = imagecolorallocate($destImage, 255, 255, 255); imagefilledrectangle($destImage, 0, 0, $width, $height, $bgc); imagecopyresampled($destImage, $sourceImage, 0, 0, $x, 0, $width, $height, $w, $h); if (function_exists('imagegif')) { // Pour GIF header('Content-Type: image/gif'); imagegif($destImage, $destination_image_url, $quality); } imagedestroy($destImage); } else { echo 'unable to load image source'; } }
/** * Init Image and color background */ private function init() { if ($this->im === null) { $this->im = imagecreatetruecolor($this->w, $this->h) or die('Can\'t Initialize the GD Libraty'); imagefilledrectangle($this->im, 0, 0, $this->w - 1, $this->h - 1, $this->color->allocate($this->im)); } }
/** * @param Thumbnail $thumbnail * @return void * @throws Exception\NoThumbnailAvailableException */ public function refresh(Thumbnail $thumbnail) { $temporaryPathAndFilename = null; try { $filename = pathinfo($thumbnail->getOriginalAsset()->getResource()->getFilename(), PATHINFO_FILENAME); $temporaryLocalCopyFilename = $thumbnail->getOriginalAsset()->getResource()->createTemporaryLocalCopy(); $temporaryPathAndFilename = $this->environment->getPathToTemporaryDirectory() . uniqid('ProcessedFontThumbnail-') . '.' . $filename . '.jpg'; $width = 1000; $height = 1000; $im = imagecreate($width, $height); $red = imagecolorallocate($im, 0xff, 0xff, 0xff); $black = imagecolorallocate($im, 0x0, 0x0, 0x0); imagefilledrectangle($im, 0, 0, 1000, 1000, $red); imagefttext($im, 48, 0, 80, 150, $black, $temporaryLocalCopyFilename, 'Neos Font Preview'); imagefttext($im, 32, 0, 80, 280, $black, $temporaryLocalCopyFilename, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); imagefttext($im, 32, 0, 80, 360, $black, $temporaryLocalCopyFilename, 'abcdefghijklmopqrstuvwxyz'); imagefttext($im, 32, 0, 80, 440, $black, $temporaryLocalCopyFilename, '1234567890'); imagefttext($im, 32, 0, 80, 560, $black, $temporaryLocalCopyFilename, '+ " * ç % & / ( ) = ? @ €'); imagejpeg($im, $temporaryPathAndFilename); $resource = $this->resourceManager->importResource($temporaryPathAndFilename); $processedImageInfo = $this->resize($thumbnail, $resource); $thumbnail->setResource($processedImageInfo['resource']); $thumbnail->setWidth($processedImageInfo['width']); $thumbnail->setHeight($processedImageInfo['height']); Files::unlink($temporaryPathAndFilename); } catch (\Exception $exception) { Files::unlink($temporaryPathAndFilename); $filename = $thumbnail->getOriginalAsset()->getResource()->getFilename(); $sha1 = $thumbnail->getOriginalAsset()->getResource()->getSha1(); $message = sprintf('Unable to generate thumbnail for the given font (filename: %s, SHA1: %s)', $filename, $sha1); throw new Exception\NoThumbnailAvailableException($message, 1433109653, $exception); } }
function create_image() { $image = imagecreatetruecolor(200, 50); $background_color = imagecolorallocate($image, 255, 255, 255); imagefilledrectangle($image, 0, 0, 200, 50, $background_color); $line_color = imagecolorallocate($image, 255, 0, 255); for ($i = 0; $i < 10; $i++) { imageline($image, 0, rand() % 50, 200, rand() % 25, $line_color); } $pixel_color = imagecolorallocate($image, 255, 0, 255); for ($i = 0; $i < 1000; $i++) { imagesetpixel($image, rand() % 200, rand() % 50, $pixel_color); } $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; $len = strlen($letters); $letter = $letters[rand(0, $len - 1)]; $text_color = imagecolorallocate($image, 0, 0, 0); $word = ""; for ($i = 0; $i < 6; $i++) { $letter = $letters[rand(0, $len - 1)]; imagestring($image, 5, 5 + $i * 30, 20, $letter, $text_color); $word .= $letter; } $_SESSION['security_key'] = $word; imagepng($image); }
function create_captcha($time) { @session_start(); $image; global $image; $word_1 = ''; $word_2 = ""; for ($i = 0; $i < 4; $i++) { $word_1 .= chr(rand(97, 122)); } for ($i = 0; $i < 4; $i++) { $word_2 .= chr(rand(97, 122)); } $_SESSION['random_number'] = $word_1 . ' ' . $word_2; $dir = './fonts/'; $image = imagecreatetruecolor(165, 50); $font = "recaptchaFont.ttf"; // font style $color = imagecolorallocate($image, 0, 0, 0); // color $white = imagecolorallocate($image, 255, 255, 255); // background color white imagefilledrectangle($image, 0, 0, 709, 99, $white); imagettftext($image, 22, 0, 5, 30, $color, $dir . $font, $_SESSION['random_number']); $_SESSION['count'] = $_SESSION['random_number']; $_SESSION['captcha_string'] = $_SESSION['random_number']; $images = glob("./style/captcha/*.png"); foreach ($images as $image_to_delete) { @unlink($image_to_delete); } imagepng($image, "./style/captcha/" . $time . ".png"); //return array('image'=>$image,'count'=>$_SESSION['count']); }
private function generateImage() { // prepare image $this->generatedImage = imagecreatetruecolor($this->getPixelRatio() * 5, $this->getPixelRatio() * 5); $rgbBackgroundColor = $this->getBackgroundColor(); if (null === $rgbBackgroundColor) { $background = imagecolorallocate($this->generatedImage, 0, 0, 0); imagecolortransparent($this->generatedImage, $background); } else { $background = imagecolorallocate($this->generatedImage, $rgbBackgroundColor[0], $rgbBackgroundColor[1], $rgbBackgroundColor[2]); imagefill($this->generatedImage, 0, 0, $background); } // prepage color $rgbColor = $this->getColor(); $gdColor = imagecolorallocate($this->generatedImage, $rgbColor[0], $rgbColor[1], $rgbColor[2]); // draw content foreach ($this->getArrayOfSquare() as $lineKey => $lineValue) { foreach ($lineValue as $colKey => $colValue) { if (true === $colValue) { imagefilledrectangle($this->generatedImage, $colKey * $this->getPixelRatio(), $lineKey * $this->getPixelRatio(), ($colKey + 1) * $this->getPixelRatio(), ($lineKey + 1) * $this->getPixelRatio(), $gdColor); } } } return $this; }
/** * 生成验证码 * @param int $width 验证码图片宽度.默认130 * @param int $height 验证码图片高度.默认40 * @param int $fontSize 验证码字体大小.默认20 * @param int $length 验证码字符个数.默认4 * @return string 验证码中的字符串 */ public static function getCaptcha($width = '130', $height = '40', $fontSize = '20', $length = '4') { $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; $randStr = substr(str_shuffle($chars), 0, $length); $image = imagecreatetruecolor($width, $height); // 定义背景色 $bgColor = imagecolorallocate($image, 0xff, 0xff, 0xff); // 定义文字及边框颜色 $blackColor = imagecolorallocate($image, 0x0, 0x0, 0x0); //生成矩形边框 imagefilledrectangle($image, 0, 0, $width, $height, $bgColor); // 循环生成雪花点 for ($i = 0; $i < 200; $i++) { $grayColor = imagecolorallocate($image, 128 + rand(0, 128), 128 + rand(0, 128), 128 + rand(0, 128)); imagesetpixel($image, rand(1, $width - 2), rand(4, $height - 2), $grayColor); } $font = ROOT_PATH . 'resources/fonts/acidic.ttf'; // 把随机字符串输入图片 $i = -1; while (isset($randStr[++$i])) { $fontColor = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100)); if (!function_exists('imagettftext')) { imagechar($image, $fontSize, 15 + $i * 30, rand(5, 20), $randStr[$i], $fontColor); } else { imagettftext($image, $fontSize, 0, 10 + $i * 30, rand(25, 35), $fontColor, $font, $randStr[$i]); } } imagepng($image); $image = $bgColor = $blackColor = $grayColor = $fontColor = null; return $randStr; }
protected function CreateCaptchaCanvas() { // Create the image $this->image = imagecreatetruecolor($this->_conf['width'], $this->_conf['height']); $white = imagecolorallocate($this->image, 255, 255, 255); imagefilledrectangle($this->image, 0, 0, $this->_conf['width'], $this->_conf['height'], $white); }
public function generate() { foreach ($this->__shapes as $shape) { $type = array_shift($shape); $color = array_shift($shape); $color = $this->_owner->imagecolorallocate(!isset($color) || is_null($color) ? $this->__base_color : $color, $this->__base_alpha); switch ($type) { case self::LINE: imageline($this->_owner->image, $shape[0], $shape[1], $shape[2], $shape[3], $color); break; case self::RECTANGLE: imagerectangle($this->_owner->image, $shape[0], $shape[1], $shape[2], $shape[3], $color); break; case self::FILLED_RECTANGLE: imagefilledrectangle($this->_owner->image, $shape[0], $shape[1], $shape[2], $shape[3], $color); break; case self::ELLIPSE: imageellipse($this->_owner->image, $shape[0], $shape[1], $shape[2], $shape[3], $color); break; case self::FILLED_ELLIPSE: imagefilledellipse($this->_owner->image, $shape[0], $shape[1], $shape[2], $shape[3], $color); break; case self::SPIRAL: $angle = $r = 0; while ($r <= $shape[2]) { imagearc($this->_owner->image, $shape[0], $shape[1], $r, $r, $angle - $shape[3], $angle, $color); $angle += $shape[3]; $r++; } break; } } return true; }
public function resize($width = 0, $height = 0) { if (!$this->info['width'] || !$this->info['height']) { return; } $scale = min($width / $this->info['width'], $height / $this->info['height']); if ($scale == 1 && $this->info['mime'] != 'image/png') { return; } $new_width = (int) ($this->info['width'] * $scale); $new_height = (int) ($this->info['height'] * $scale); $xpos = (int) (($width - $new_width) / 2); $ypos = (int) (($height - $new_height) / 2); $image_old = $this->image; $this->image = imagecreatetruecolor($width, $height); if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') { imagealphablending($this->image, false); imagesavealpha($this->image, true); $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127); imagecolortransparent($this->image, $background); } else { $background = imagecolorallocate($this->image, 255, 255, 255); } imagefilledrectangle($this->image, 0, 0, $width, $height, $background); imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']); imagedestroy($image_old); $this->info['width'] = $width; $this->info['height'] = $height; }
function img_bundle() { $sources = func_get_args(); $target = array_shift($sources); $this->sizeinfo = array(); $this->vpos = 0; $this->wpos = 0; foreach ($sources as $src) { $this->_bundle_index($src); } $target_img = imagecreatetruecolor($this->wpos, $this->vpos); $bg_color = imagecolorallocate($target_img, 255, 0, 255); //牺牲掉这个最丑的颜色做透明背景 //todo:智能选择调色板中没有的颜色 imagefilledrectangle($target_img, 0, 0, $this->wpos, $this->vpos, $bg_color); $app = $params['app'] ? app::get($params['app']) : $this->app; foreach ($this->sizeinfo as $file => $info) { $src_img = imagecreatefromgif($app->res_dir . '/' . $file); $rst = imagecopy($target_img, $src_img, 0, $info[0], 0, 0, $info[1], $info[2]); // $rst = imagecopyresampled($target_img,$src_img,0,$info[0],0,0,$info[1],$info[2],$info[1],$info[2]); // $rst = imagecopyresized($target_img,$src_img,0,$info[0],0,0,$info[1],$info[2],$info[1],$info[2]); $src_img = null; } imagecolortransparent($target_img, $bg_color); // imagetruecolortopalette($target_img,true,256); //todo:优化显示效果 imagegif($target_img, $app->res_dir . '/' . $target); $target_img = null; $rs = fopen($app->res_dir . '/' . $target, 'a'); $info = serialize($this->sizeinfo); fwrite($rs, pack('a*V', $info, strlen($info))); fclose($rs); $this->sizeinfo = null; }
function doImage() { if ($this->use_transparent_text == true || $this->bgimg != "") { $this->im = imagecreatetruecolor($this->image_width, $this->image_height); $bgcolor = imagecolorallocate($this->im, hexdec(substr($this->image_bg_color, 1, 2)), hexdec(substr($this->image_bg_color, 3, 2)), hexdec(substr($this->image_bg_color, 5, 2))); imagefilledrectangle($this->im, 0, 0, imagesx($this->im), imagesy($this->im), $bgcolor); } else { $this->im = imagecreate($this->image_width, $this->image_height); $bgcolor = imagecolorallocate($this->im, hexdec(substr($this->image_bg_color, 1, 2)), hexdec(substr($this->image_bg_color, 3, 2)), hexdec(substr($this->image_bg_color, 5, 2))); } if ($this->bgimg != "") { $this->setBackground(); } $this->createCode(); if (!$this->draw_lines_over_text && $this->draw_lines) { $this->drawLines(); } $this->drawWord(); if ($this->arc_linethrough == true) { $this->arcLines(); } if ($this->draw_lines_over_text && $this->draw_lines) { $this->drawLines(); } $this->output(); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 10, $sses_name = "verify") { // 创建画布 $width = 80; $height = 28; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $balck = imagecolorallocate($image, 0, 0, 0); // 用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sses_name] = $chars; for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 3 + $i * $size; $y = mt_rand(18, 22); $fontfile = "../fonts/" . "STFANGSO.TTF"; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } // 加点 for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $balck); } // 加直线 for ($i = 0; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } header("content-type:image/jpg"); imagegif($image); imagedestroy($image); }
public function testImage() { $size = 300; $this->image = imagecreatetruecolor($size, $size); //用白色背景,黑色边框画方框 $back = imagecolorallocate($this->image, 255, 255, 255); $border = imagecolorallocate($this->image, 0, 0, 0); imagefilledrectangle($this->image, 0, 0, $size, $size, $back); //画出白色背景 imagerectangle($this->image, 0, 0, $size - 1, $size - 1, $border); //画出黑色边框 $yellow_x = 150; $yellow_y = 75; $red_x = 100; $red_y = 160; $blue_x = 200; $blue_y = 160; $radius = 150; $yellow = imagecolorallocatealpha($this->image, 255, 255, 0, 75); //此函数将黄色的alpha值调为75,就是透明度 $red = imagecolorallocatealpha($this->image, 255, 0, 0, 75); $blue = imagecolorallocatealpha($this->image, 0, 0, 255, 75); //画三个交叠的圆 imagefilledellipse($this->image, $yellow_x, $yellow_y, $radius, $radius, $yellow); //此函数就是我要在$image上画一个圆心($yellow_x,$yellow_y)半径为$radius/2颜色为$yellow的圆 imagefilledellipse($this->image, $red_x, $red_y, $radius, $radius, $red); imagefilledellipse($this->image, $blue_x, $blue_y, $radius, $radius, $blue); //输出正确的header header("Content-type: image/png"); //输出结果 imagepng($this->image); imagedestroy($this->image); }
/** * @param integer $line_no */ public function drawBar($line_no) { $arrPointList = $this->arrPointList[$line_no]; // データ数を数える $count = count($arrPointList); // 半目盛りの幅を求める $half_scale = intval($this->area_width / ($count + 1) / 2); // 目盛りの幅を求める $scale_width = intval($this->area_width / ($count + 1)); // 棒グラフのサイズを求める $bar_width = intval(($scale_width - BAR_PAD * 2) / $this->line_max); // 色数の取得 $c_max = count($this->arrColor); for ($i = 0; $i < $count; $i++) { $left = $arrPointList[$i][0] - $half_scale + BAR_PAD + $bar_width * $line_no; $top = $arrPointList[$i][1]; $right = $left + $bar_width; $bottom = $this->top + $this->area_height; // 影の描画 if ($this->shade_on) { imagefilledrectangle($this->image, $left + 2, $top + 2, $right + 2, $bottom, $this->shade_color); } //imagefilledrectangle($this->image, $left, $top, $right, $bottom, $this->arrColor[($i % $c_max)]); imagefilledrectangle($this->image, $left, $top, $right, $bottom, $this->arrColor[$line_no]); imagerectangle($this->image, $left, $top, $right, $bottom, $this->flame_color); } }
function getDataMatrix($code) { $x = 70; // barcode center $y = 70; // barcode center $height = 140; // barcode height in 1D ; module size in 2D $width = 4; // barcode height in 1D ; not use in 2D $angle = 90; // rotation in degrees : nb : non horizontable barcode might not be usable because of pixelisation $type = 'datamatrix'; $im = imagecreatetruecolor(140, 140); $black = ImageColorAllocate($im, 0x0, 0x0, 0x0); $white = ImageColorAllocate($im, 0xff, 0xff, 0xff); $red = ImageColorAllocate($im, 0xff, 0x0, 0x0); $blue = ImageColorAllocate($im, 0x0, 0x0, 0xff); imagefilledrectangle($im, 0, 0, 140, 140, $white); $data = \BarcodeGEN::gd($im, $black, $x, $y, $angle, $type, array('code' => $code), $width); /*for($i=1; $i<5; $i++){ drawCross($im, $blue, $data['p'.$i]['x'], $data['p'.$i]['y']); }*/ return $im; }
/** * Render the caption. */ public function render() { // Get graphical obects $img = $this->plot->getImg(); $palette = $this->plot->getPalette(); $text = $this->plot->getText(); $primitive = $this->plot->getPrimitive(); // Get the caption area $captionArea = $this->plot->getCaptionArea(); // Get the pie color set $colorSet = $this->colorSet; $colorSet->reset(); $i = 0; foreach ($this->labelList as $label) { // Get the next color $color = $colorSet->currentColor(); $colorSet->next(); $boxX1 = $captionArea->x1; $boxX2 = $boxX1 + $this->labelBoxWidth; $boxY1 = $captionArea->y1 + 5 + $i * ($this->labelBoxHeight + 5); $boxY2 = $boxY1 + $this->labelBoxHeight; $primitive->outlinedBox($boxX1, $boxY1, $boxX2, $boxY2, $palette->axisColor[0], $palette->axisColor[1]); imagefilledrectangle($img, $boxX1 + 2, $boxY1 + 2, $boxX2 - 2, $boxY2 - 2, $color->getColor($img)); $text->printText($img, $boxX2 + 5, $boxY1 + $this->labelBoxHeight / 2, $this->plot->getTextColor(), $label, $text->fontCondensed, $text->VERTICAL_CENTER_ALIGN); $i++; } }
/** * Returns a mask * * @param \WideImage\Image $image * @return \WideImage\Image */ public function execute($image) { $width = $image->getWidth(); $height = $image->getHeight(); $mask = TrueColorImage::create($width, $height); $mask->setTransparentColor(-1); $mask->alphaBlending(false); $mask->saveAlpha(false); for ($i = 0; $i <= 255; $i++) { $greyscale[$i] = ImageColorAllocate($mask->getHandle(), $i, $i, $i); } imagefilledrectangle($mask->getHandle(), 0, 0, $width, $height, $greyscale[255]); $transparentColor = $image->getTransparentColor(); $alphaToGreyRatio = 255 / 127; for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $color = $image->getColorAt($x, $y); if ($color == $transparentColor) { $rgba['alpha'] = 127; } else { $rgba = $image->getColorRGB($color); } imagesetpixel($mask->getHandle(), $x, $y, $greyscale[255 - round($rgba['alpha'] * $alphaToGreyRatio)]); } } return $mask; }
/** * Return label image for product * @param <type> $productId */ public function getImage($productId) { $product = Mage::getModel('catalog/product')->load($productId); //create base image $labelSize = Mage::helper('BarcodeLabel')->getlabelSize(); $height = $labelSize['height'] * $this->_coef; $width = $labelSize['width'] * $this->_coef; $im = imagecreatetruecolor($width, $height); $white = imagecolorallocate($im, 255, 255, 255); imagefilledrectangle($im, 0, 0, $width, $height, $white); //add barcode $this->addBarcode($im, $product); //add product name $this->addName($im, $product); //add product attributes $this->addProductAttributes($im, $product); //add manufacturer $this->addManufacturer($im, $product); //add logo $this->addLogo($im); // add product image $this->addProductPicture($im, $product); //add product sku $this->addSku($im, $product); //add price $this->addPrice($im, $product); //add price $this->addCustoms($im, $product); //return image return $im; }
function create($string, $blocks = 5, $size = 400) { $togenerate = ceil($blocks / 2); $hash = md5($string); $hashsize = $blocks * $togenerate; $hash = str_pad($hash, $hashsize, $hash); $color = substr($hash, 0, 6); $blocksize = $size / $blocks; $image = imagecreate($size, $size); $bg = imagecolorallocate($image, 255, 255, 255); $color = imagecolorallocate($image, hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2))); for ($x = 0; $x < $blocks; $x++) { for ($y = 0; $y < $blocks; $y++) { if ($x < $togenerate) { $pixel = hexdec($hash[$x * $blocks + $y]) % 2 == 0; } else { $pixel = hexdec($hash[($blocks - 1 - $x) * $blocks + $y]) % 2 == 0; } $pixelcolor = $bg; if ($pixel) { $pixelcolor = $color; } imagefilledrectangle($image, $x * $blocksize, $y * $blocksize, ($x + 1) * $blocksize, ($y + 1) * $blocksize, $pixelcolor); } } header('Content-type: image/png'); imagepng($image); }
function draw_bar_graph($width, $height, $data, $max_value, $filename) { // Create the empty graph image $img = imagecreatetruecolor($width, $height); // Set a white background with black text and gray graphics $bg_color = imagecolorallocate($img, 255, 255, 255); // white $text_color = imagecolorallocate($img, 255, 255, 255); // white $bar_color = imagecolorallocate($img, 0, 0, 0); // black $border_color = imagecolorallocate($img, 192, 192, 192); // light gray // Fill the background imagefilledrectangle($img, 0, 0, $width, $height, $bg_color); // Draw the bars $bar_width = $width / (count($data) * 2 + 1); for ($i = 0; $i < count($data); $i++) { imagefilledrectangle($img, $i * $bar_width * 2 + $bar_width, $height, $i * $bar_width * 2 + $bar_width * 2, $height - $height / $max_value * $data[$i][1], $bar_color); imagestringup($img, 5, $i * $bar_width * 2 + $bar_width, $height - 5, $data[$i][0], $text_color); } // Draw a rectangle around the whole thing imagerectangle($img, 0, 0, $width - 1, $height - 1, $border_color); // Draw the range up the left side of the graph for ($i = 1; $i <= $max_value; $i++) { imagestring($img, 5, 0, $height - $i * ($height / $max_value), $i, $bar_color); } // Write the graph image to a file imagepng($img, $filename, 5); imagedestroy($img); }
function getCode($num, $w, $h) { // 去掉了 0 1 O l 等 $str = "23456789abcdefghijkmnpqrstuvwxyz"; $code = ''; for ($i = 0; $i < $num; $i++) { $code .= $str[mt_rand(0, strlen($str) - 1)]; } //将生成的验证码写入session,备验证页面使用 $_SESSION["my_checkcode"] = $code; //创建图片,定义颜色值 Header("Content-type: image/PNG"); $im = imagecreate($w, $h); $black = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); $gray = imagecolorallocate($im, 118, 151, 199); $bgcolor = imagecolorallocate($im, 235, 236, 237); //画背景 imagefilledrectangle($im, 0, 0, $w, $h, $bgcolor); //画边框 imagerectangle($im, 0, 0, $w - 1, $h - 1, $gray); //imagefill($im, 0, 0, $bgcolor); //在画布上随机生成大量点,起干扰作用; for ($i = 0; $i < 80; $i++) { imagesetpixel($im, rand(0, $w), rand(0, $h), $black); } //将字符随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 $strx = rand(5, 10); for ($i = 0; $i < $num; $i++) { $strpos = rand(1, 6); imagestring($im, 20, $strx, $strpos, substr($code, $i, 1), $black); $strx += $w / 5; } imagepng($im); imagedestroy($im); }
private static function set_img() { self::$res_img = imagecreatetruecolor(self::$width, self::$height); $color_bg = imagecolorallocate(self::$res_img, 255, 255, 255); //填充图片 imagefilledrectangle(self::$res_img, 0, self::$height, self::$width, 0, $color_bg); }
/** * Background fill an image using the provided color * * @param int $width The desired width of the new image * @param int $height The desired height of the new image * @param Array the desired pad colors in RGB format, array should be array( 'top' => '', 'right' => '', 'bottom' => '', 'left' => '' ); */ private function fill_color(array $colors) { $current_size = $this->editor->get_size(); $size = array('width' => $this->args['width'], 'height' => $this->args['height']); $offsetLeft = ($size['width'] - $current_size['width']) / 2; $offsetTop = ($size['height'] - $current_size['height']) / 2; $new_image = imagecreatetruecolor($size['width'], $size['height']); // This is needed to support alpha imagesavealpha($new_image, true); imagealphablending($new_image, false); // Check if we are padding vertically or horizontally if ($current_size['width'] != $size['width']) { $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['left'], 0, 3), substr($colors['left'], 3, 3), substr($colors['left'], 6, 3), substr($colors['left'], 9, 3)); // Fill left color imagefilledrectangle($new_image, 0, 0, $offsetLeft + 5, $size['height'], $colorToPaint); $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['right'], 0, 3), substr($colors['right'], 3, 3), substr($colors['right'], 6, 3), substr($colors['left'], 9, 3)); // Fill right color imagefilledrectangle($new_image, $offsetLeft + $current_size['width'] - 5, 0, $size['width'], $size['height'], $colorToPaint); } elseif ($current_size['height'] != $size['height']) { $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['top'], 0, 3), substr($colors['top'], 3, 3), substr($colors['top'], 6, 3), substr($colors['left'], 9, 3)); // Fill top color imagefilledrectangle($new_image, 0, 0, $size['width'], $offsetTop + 5, $colorToPaint); $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['bottom'], 0, 3), substr($colors['bottom'], 3, 3), substr($colors['bottom'], 6, 3), substr($colors['left'], 9, 3)); // Fill bottom color imagefilledrectangle($new_image, 0, $offsetTop - 5 + $current_size['height'], $size['width'], $size['height'], $colorToPaint); } imagecopy($new_image, $this->editor->get_image(), $offsetLeft, $offsetTop, 0, 0, $current_size['width'], $current_size['height']); $this->editor->update_image($new_image); $this->editor->update_size(); }
/** * Génère l'avatar */ public function run() { //On créer l'image avec les dimentions données $image = imagecreate($this->_size, $this->_size); //On créer la couleur en fonction du hash de la chaine de caractères $color = imagecolorallocate($image, hexdec(substr($this->_color, 0, 2)), hexdec(substr($this->_color, 2, 2)), hexdec(substr($this->_color, 4, 2))); //on défini le fond de l'image (blanc) $bg = imagecolorallocate($image, 255, 255, 255); //nombre de blocs à placer dans l'image (taille de l'image/taille des blocs) $c = $this->_size / $this->_blockSize; for ($x = 0; $x < ceil($c / 2); $x++) { for ($y = 0; $y < $c; $y++) { // Si le nombre est pair $pixel vaut true sinon $pixel vaut false $pixel = hexdec($this->_hash[(int) ($x * ceil($c / 2)) + $y]) % 2 == 0; if ($pixel) { $pixelColor = $color; } else { $pixelColor = $bg; } // On place chaque bloc de l'image //imagefilledrectangle($image, $x*$this->_blockSize, $y*$this->_blockSize, ($x+1)*$this->_blockSize, ($y+1)*$this->_blockSize, $pixelColor); //imagefilledrectangle($image, $this->_size-$x*$this->_blockSize, $y*$this->_blockSize, $this->_size-($x+1)*$this->_blockSize, ($y+1)*$this->_blockSize, $pixelColor); imagefilledrectangle($image, $x * $this->_blockSize, $y * $this->_blockSize, ($x + 1) * $this->_blockSize, ($y + 1) * $this->_blockSize, $pixelColor); imagefilledrectangle($image, $this->_size - $x * $this->_blockSize, $y * $this->_blockSize, $this->_size - ($x + 1) * $this->_blockSize, ($y + 1) * $this->_blockSize, $pixelColor); } } ob_start(); imagepng($image); //on place l'image en mémoire $this->_image = ob_get_contents(); ob_clean(); }