/** * * @brief 图片处理 验证码 * @param unknown_type $length * @param unknown_type $mode * @param unknown_type $type * @param unknown_type $width * @param unknown_type $height * @param unknown_type $verifyName */ public static function buildImageVerify($length = 4, $mode = 1, $type = 'png', $width = 48, $height = 22, $verifyName = 'verify') { $randval = self::randString($length, $mode); $_SESSION[$verifyName] = md5($randval); $width = $length * 10 + 10 > $width ? $length * 10 + 10 : $width; if ($type != 'gif' && function_exists('imagecreatetruecolor')) { $im = imagecreatetruecolor($width, $height); } else { $im = imagecreate($width, $height); } $r = array(225, 255, 255, 223); $g = array(225, 236, 237, 255); $b = array(225, 236, 166, 125); $key = mt_rand(0, 3); $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(随机) $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色 imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor); imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor); $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); // 干扰 for ($i = 0; $i < 10; $i++) { imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(200, 300), mt_rand(35, 200), 55, 44, $stringColor); } for ($i = 0; $i < 25; $i++) { imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor); } for ($i = 0; $i < $length; $i++) { imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval[$i], $stringColor); } Image::output($im, $type); }
/** * 显示服务器图像文件 支持URL方式 * @param string $imgFile 图像文件名 * @param string $text 文字字符串 * @param string $width 图像宽度 * @param string $height 图像高度 * @return void */ public static function showImg($imgFile, $text = '', $width = 80, $height = 30) { //获取图像文件信息 $info = Image::getImageInfo($imgFile); if ($info !== false) { //修改读取bmp图片 $createFun = str_replace('/', 'createfrom', $info['mime']); if (strrpos($info['mime'], 'bmp')) { $strImg = ""; $strImg = file_get_contents($imgFile); header("Content-type: " . $info['mime']); echo $strImg; return; } $im = $createFun($imgFile); if ($im) { $ImageFun = str_replace('/', '', $info['mime']); if (!empty($text)) { $tc = imagecolorallocate($im, 0, 0, 0); imagestring($im, 3, 5, 5, $text, $tc); } if ($info['type'] == 'png' || $info['type'] == 'gif') { imagealphablending($im, false); //取消默认的混色模式 imagesavealpha($im, true); //设定保存完整的 alpha 通道信息 } header("Content-type: " . $info['mime']); $ImageFun($im); imagedestroy($im); return; } } //获取或者创建图像文件失败则生成空白PNG图片 $im = imagecreatetruecolor($width, $height); $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); imagestring($im, 4, 5, 5, "NO PIC", $tc); Image::output($im); return; }
/** * 生成UPC-A条形码 * @static * @param string $type 图像格式 * @param string $type 图像格式 * @param string $lw 单元宽度 * @param string $hi 条码高度 * @return string */ static function UPCA($code, $type = 'png', $lw = 2, $hi = 100) { static $Lencode = array('0001101', '0011001', '0010011', '0111101', '0100011', '0110001', '0101111', '0111011', '0110111', '0001011'); static $Rencode = array('1110010', '1100110', '1101100', '1000010', '1011100', '1001110', '1010000', '1000100', '1001000', '1110100'); $ends = '101'; $center = '01010'; /* UPC-A Must be 11 digits, we compute the checksum. */ if (strlen($code) != 11) { die("UPC-A Must be 11 digits."); } /* Compute the EAN-13 Checksum digit */ $ncode = '0' . $code; $even = 0; $odd = 0; for ($x = 0; $x < 12; $x++) { if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; } } $code .= (10 - ($odd * 3 + $even) % 10) % 10; /* Create the bar encoding using a binary string */ $bars = $ends; $bars .= $Lencode[$code[0]]; for ($x = 1; $x < 6; $x++) { $bars .= $Lencode[$code[$x]]; } $bars .= $center; for ($x = 6; $x < 12; $x++) { $bars .= $Rencode[$code[$x]]; } $bars .= $ends; /* Generate the Barcode Image */ if ($type != 'gif' && function_exists('imagecreatetruecolor')) { $im = imagecreatetruecolor($lw * 95 + 30, $hi + 30); } else { $im = imagecreate($lw * 95 + 30, $hi + 30); } $fg = ImageColorAllocate($im, 0, 0, 0); $bg = ImageColorAllocate($im, 255, 255, 255); ImageFilledRectangle($im, 0, 0, $lw * 95 + 30, $hi + 30, $bg); $shift = 10; for ($x = 0; $x < strlen($bars); $x++) { if ($x < 10 || $x >= 45 && $x < 50 || $x >= 85) { $sh = 10; } else { $sh = 0; } if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; } ImageFilledRectangle($im, $x * $lw + 15, 5, ($x + 1) * $lw + 14, $hi + 5 + $sh, $color); } /* Add the Human Readable Label */ ImageString($im, 4, 5, $hi - 5, $code[0], $fg); for ($x = 0; $x < 5; $x++) { ImageString($im, 5, $lw * (13 + $x * 6) + 15, $hi + 5, $code[$x + 1], $fg); ImageString($im, 5, $lw * (53 + $x * 6) + 15, $hi + 5, $code[$x + 6], $fg); } ImageString($im, 4, $lw * 95 + 17, $hi - 5, $code[11], $fg); /* Output the Header and Content. */ Image::output($im, $type); }
public static function UPCA($code, $type = 'png', $lw = 2, $hi = 100) { static $Lencode = array('0001101', '0011001', '0010011', '0111101', '0100011', '0110001', '0101111', '0111011', '0110111', '0001011'); static $Rencode = array('1110010', '1100110', '1101100', '1000010', '1011100', '1001110', '1010000', '1000100', '1001000', '1110100'); $ends = '101'; $center = '01010'; if (strlen($code) != 11) { exit('UPC-A Must be 11 digits.'); } $ncode = '0' . $code; $even = 0; $odd = 0; for ($x = 0; $x < 12; $x++) { if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; } } $code .= (10 - ($odd * 3 + $even) % 10) % 10; $bars = $ends; $bars .= $Lencode[$code[0]]; for ($x = 1; $x < 6; $x++) { $bars .= $Lencode[$code[$x]]; } $bars .= $center; for ($x = 6; $x < 12; $x++) { $bars .= $Rencode[$code[$x]]; } $bars .= $ends; if ($type != 'gif' && function_exists('imagecreatetruecolor')) { $im = imagecreatetruecolor($lw * 95 + 30, $hi + 30); } else { $im = imagecreate($lw * 95 + 30, $hi + 30); } $fg = ImageColorAllocate($im, 0, 0, 0); $bg = ImageColorAllocate($im, 255, 255, 255); ImageFilledRectangle($im, 0, 0, $lw * 95 + 30, $hi + 30, $bg); $shift = 10; for ($x = 0; $x < strlen($bars); $x++) { if ($x < 10 || 45 <= $x && $x < 50 || 85 <= $x) { $sh = 10; } else { $sh = 0; } if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; } ImageFilledRectangle($im, $x * $lw + 15, 5, ($x + 1) * $lw + 14, $hi + 5 + $sh, $color); } ImageString($im, 4, 5, $hi - 5, $code[0], $fg); for ($x = 0; $x < 5; $x++) { ImageString($im, 5, $lw * (13 + $x * 6) + 15, $hi + 5, $code[$x + 1], $fg); ImageString($im, 5, $lw * (53 + $x * 6) + 15, $hi + 5, $code[$x + 6], $fg); } ImageString($im, 4, $lw * 95 + 17, $hi - 5, $code[11], $fg); Image::output($im, $type); }
<?php require 'Image.class.php'; $image = new Image('image.jpg'); // $image->type = IMAGETYPE_JPEG; // change the output type of the image, using PHP's constants // $image->width = 400; // aspect resizes based on width // $image->x = 400; // aspect resizes based on width // $image->height = 300; // aspect resizes based on height // $image->y = 300; // aspect resizes based on height $image->scale(400, 300); // scales the image but maintains the aspect ratio // $image->scale(400, 300, array('force' => true)); // scales the image and forces the size, can lose the aspect ratio // $image->cutout(600, 200); // cuts out a section of the image resizes to the minimum possible size unless options are specified // $image->cutout(600, 200, array('scaleX' => 800, 'scaleY' => 600, 'offsetX' => 0, 'offsetY' => 0)); // cuts out the top left quarter of the image // $image->whitespace(400, 300); // resizes the image and adds whitespace using the default colour // $image->whitespace(400, 300, array('color' => '#f00')); // resizes the image and adds whitespace using a custom colour // $image->whitespace(400, 300, array('image' => 'bg.jpg')); // resizes the image and adds whitespace using an image as the base // $image->watermark('sample.png'); // watermarks the image using the specified watermark image // $image->watermark('sample.png', array('repeatX' => false, 'repeatY' => false)); // watermarks the image using the specified watermark image, repeating the image vertically only // $image->watermark('sample.png', array('repeatX' => false, 'repeatY' => true)); // watermarks the image using the specified watermark image, repeating the image vertically only // $image->watermark('sample.png', array('repeatX' => true, 'repeatY' => false)); // watermarks the image using the specified watermark image, repeating the image horizontally only // $image->watermark('sample.png', array('repeatX' => true, 'repeatY' => true)); // watermarks the image using the specified watermark image, repeating the watermark horizontally and vertically // $image->watermark = 'sample.png'; // watermarks the image using the specified watermark image, using default options // $filename = $image->write('thumb_image'); // write the image to the specified file, using the default extension // $filename = $image->write('thumb_image.jpg', array('extension' => false)); // write the image to the specified file, but don't use the default extension // $image->drawLine(array(0, 0), array($image->currentX, $image->currentY), array('size' => 4, 'color' => '#fff')); // $image->drawBox(array(0, 230), array(400, 300), array('size' => 4, 'color' => '#000', 'transparency' => 60)); // $image->addText('Testing raw text input!', 30, ($image->currentY - 30), array('color' => '#fff', 'font' => 'ArialBlack', 'transparency' => 30)); $image->output(); // output the image to the browser (also sets the http header)
require_once('RedirectBrowserException.php'); require_once('Session.php'); require_once('URL.php'); $tmpl = new Template(); $tmpl->passedvar = "This string was passed through the Template object to be rendered in the HTML file."; $tmpl->hex = Color::RGBToHex(60, 120, 60); $tmpl->alpha = Color::HexToRGBA($tmpl->hex, .5); $tmpl->rgb = Color::HexToRGB($tmpl->hex); $img = new Image(); $img->source = 'portrait.png'; $img->Write->Normal(20, 20, "A Self Portrait of Me", 5, "#000000", 1); $img->destination = 'portrait2.png'; $img->output(); $img->clean(); unset($img); //start a session and store a variable; setSession(0,'/'); // expires with browser session, the root is '/' setSessionVar('foo', 'bar'); //there's no retrieval function, so this is kind of stupid if( !isset($_SESSION['foo']) ){ throw new RedirectBrowserException("example.php"); } //Database calls /* $db = new Database("username", "password", "database name", "location of database", "type of database"); // currently only supports "mysql" $sql = "SELECT * FROM mytable WHERE myid=?"; $values = array(4); // myid
public static function buildEmail($email, $rgb = array(), $filename = '', $type = 'png') { $mail = explode('@', $email); $user = trim($mail[0]); $mail = strtolower(trim($mail[1])); $path = dirname(__FILE__) . '/Mail/'; if (is_file($path . $mail . '.png')) { $im = imagecreatefrompng($path . $mail . '.png'); $user_width = imagettfbbox(9, 0, dirname(__FILE__) . "/Mail/tahoma.ttf", $user); $x_value = 200 - ($user_width[2] + 113); if (empty($rgb)) { $color = imagecolorallocate($im, 102, 104, 104); } else { $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]); } imagettftext($im, 9, 0, $x_value, 16, $color, dirname(__FILE__) . "/Mail/tahoma.ttf", $user); } else { $user_width = imagettfbbox(9, 0, dirname(__FILE__) . "/Mail/tahoma.ttf", $email); $width = $user_width[2] + 15; $height = 20; $im = imagecreate($width, 20); $backColor = imagecolorallocate($im, 255, 255, 255); //背景色(随机) $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色 $pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //点颜色 imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor); imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor); if (empty($rgb)) { $color = imagecolorallocate($im, 102, 104, 104); } else { $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]); } imagettftext($im, 9, 0, 5, 16, $color, dirname(__FILE__) . "/Mail/tahoma.ttf", $email); for ($i = 0; $i < 25; $i++) { imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor); } } Image::output($im, $type, $filename); }
mkdir_deep($originPath); file_put_contents($originPath, $data); $isNotExists = false; break; } } if ($isNotExists) { error404(); } } if ($params['type'] !== 'origin') { $Image = new Image(); $Image->set($originPath); $Image->{$params['type']}(intval($params['width']), intval($params['height'])); mkdir_deep($requestUrl); $Image->output($requestUrl, intval($params['quality'])); $Image->destroy(); } header('Content-type: image/jpeg'); @readfile($requestUrl); /* * ディレクトリ作成 */ function mkdir_deep($path) { $dirs = explode('/', dirname($path)); $path = ''; foreach ($dirs as $dir) { $path .= $dir; if (!is_dir($path)) { @mkdir($path);
function check_directory_blacklisted($dblink, $token_id, $session, $path, $image = false) { # Adding a slash to our path $path .= substr($path, -1, 1) == '/' ? '' : '/'; $blacklisted = false; # If we have blacklist enabled if (NQ_BLACKLIST_ENABLED) { # Checking our blackist table $query = "\tSELECT\n\t\t\t\t\t\t\t`path`\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t" . NQ_BLACKLIST_PATH_TABLE . "\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t`token_id`\t=" . (int) $token_id . " AND\n\t\t\t\t\t\t\t`path` \t\t=LEFT('" . mysqli_escape_string($dblink, $path) . "',LENGTH(`path`)) \n\t\t\t\t\t\tLIMIT 1"; $blackist_data = mysqli_single_result_query($dblink, $query); # Failing if we have a blacklisted path $blacklisted = isset($blackist_data['path']); # Checking our sessions blacklist if (!$blacklisted && isset($session->{NQ_SESSION_GROUP}->blacklist)) { # Matching any that start with our path foreach ($session->{NQ_SESSION_GROUP}->blacklist as $blacklist) { if ($blacklist == substr($path, 0, strlen($blacklist))) { $blacklisted = true; break; } } } # If we are enabling a whitelist if (NQ_WHITELIST_ENABLED) { # Checking our sessions whitelist if ($blacklisted && isset($session->{NQ_SESSION_GROUP}->whitelist)) { # Matching any that start with our path foreach ($session->{NQ_SESSION_GROUP}->whitelist as $whitelist) { if ($whitelist == substr($path, 0, strlen($whitelist))) { $blacklisted = false; break; } } } # Checking for whitelist if ($blacklisted) { # Checking our whitelist table $query = "\tSELECT\n\t\t\t\t\t\t\t\t\t`path`\n\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\t" . NQ_WHITELIST_PATH_TABLE . "\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t`token_id`\t=" . (int) $token_id . " AND\n\t\t\t\t\t\t\t\t\t`path`\t\t=LEFT('" . mysqli_escape_string($dblink, $path) . "',LENGTH(`path`)) \n\t\t\t\t\t\t\t\tLIMIT 1"; $whitelist_data = mysqli_single_result_query($dblink, $query); # Failing if we have a blacklisted path $blacklisted = !isset($whitelist_data['path']); } # Leaving if we are bad if ($blacklisted) { # We are blacklisting an image if ($image) { # Loading our blacklist image $img = new Image(); $img->load(NQ_BLACKLIST_IMAGE_FILE); # Sending our image $img->headerOutput(); $img->output(); exit; } # Non-image exit exit_fail(NQ_ERROR_INVALID_VALUE, str_replace('%path%', $path, LANG_BLACKLIST_PATH_DENIED)); } } } }
/** +---------------------------------------------------------- * 显示服务器图像文件 * 支持URL方式 +---------------------------------------------------------- * @static * @access public +---------------------------------------------------------- * @param string $imgFile 图像文件名 * @param string $text 文字字符串 * @param string $width 图像宽度 * @param string $height 图像高度 +---------------------------------------------------------- * @return void +---------------------------------------------------------- */ function showImg($imgFile, $text = '', $width = 80, $height = 30) { //获取图像文件信息 //2007/6/26 增加图片水印输出,$text为图片的完整路径即可 $info = Image::getImageInfo($imgFile); if ($info !== false) { $createFun = str_replace('/', 'createfrom', $info['mime']); $im = $createFun($imgFile); if ($im) { $ImageFun = str_replace('/', '', $info['mime']); //水印开始 if (!empty($text)) { $tc = imagecolorallocate($im, 0, 0, 0); if (is_file($text) && file_exists($text)) { // 取得水印信息 $textInfo = Image::getImageInfo($text); $createFun2 = str_replace('/', 'createfrom', $textInfo['mime']); $waterMark = $createFun2($text); $imgW = $info["width"]; $imgH = $info["width"] * $textInfo["height"] / $textInfo["width"]; $y = ($info["height"] - $textInfo["height"]) / 2; if (function_exists("ImageCopyResampled")) { ImageCopyResampled($im, $waterMark, 0, $y, 0, 0, $imgW, $imgH, $textInfo["width"], $textInfo["height"]); } else { ImageCopyResized($im, $waterMark, 0, $y, 0, 0, $imgW, $imgH, $textInfo["width"], $textInfo["height"]); } } else { imagestring($im, 3, 5, 5, $text, $tc); } //ImageDestroy($tc); } //水印结束 if ($info['type'] == 'png' || $info['type'] == 'gif') { imagealphablending($im, FALSE); //取消默认的混色模式 imagesavealpha($im, TRUE); //设定保存完整的 alpha 通道信息 } Header("Content-type: " . $info['mime']); $ImageFun($im); @ImageDestroy($im); return; } } //获取或者创建图像文件失败则生成空白PNG图片 $im = imagecreatetruecolor($width, $height); $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); imagestring($im, 4, 5, 5, "no pic", $tc); Image::output($im); return; }
$contenttype = 'jpeg'; break; } header('Content-type: image/' . $contenttype); $this->write(NULL, $out); } /*** * Kill self */ function destroy() { imagedestroy($this->res); $this->source = NULL; $this->contenttype = NULL; } } if (!function_exists('exif_imagetype')) { function exif_imagetype($f) { if (false !== (list(, , $type, ) = getimagesize($f))) { return $type; } return IMAGETYPE_PNG; // meh } } $image = $_GET['image']; $I = new Image($image); $I->flip_v(); $I->output();
<?php require 'Image.php'; $img = new Image('dansje.png'); $img->background_color = array(255, 255, 255); //$img->resize(0, 150); //$img->save('created/darude.png', IMAGETYPE_PNG, array('chmod' => 0777)); $img->max(500, 300); $img->output(IMAGETYPE_PNG);