public function generate(array $array) { $frames = array(); $frame_delay = array(); foreach ($array['frames'] as $frame) { $image = $this->_createImage($frame['image']); if (array_key_exists('text', $frame)) { foreach ($frame['text'] as $text) { // Set defaults $defaults = array("angle" => $this->_defaultAngle, "fonts" => $this->_defaultFont, "fonts-color" => $this->_defaultFontColor, "fonts-size" => $this->_defaultFontSize, "y-position" => $this->_defaultYPosition, "x-position" => $this->_defaultXPosition, "text" => null, "letter-spacing" => 0); // Overwrite all the defaults with the arguments $args = array_merge($defaults, $text); $fontColor = is_array($args['fonts-color']) ? $args['fonts-color'] : $this->_hex2rgb($args['fonts-color']); $text_color = imagecolorallocate($image, $fontColor[0], $fontColor[1], $fontColor[2]); $this->imagettftextSp($image, $args['fonts-size'], $args['angle'], $args['x-position'], $args['y-position'], $text_color, $args['fonts'], $args['text'], $args['letter-spacing']); } } $delay = array_key_exists('delay', $frame) ? $frame['delay'] : $this->_defaultDelay; ob_start(); imagegif($image); $frames[] = ob_get_contents(); $frame_delay[] = $delay; // Delay in the animation. ob_end_clean(); } $repeat = array_key_exists('repeat', $array) ? $array['repeat'] : $this->_defaultRepeat; $gif = new GIFEncoder($frames, $frame_delay, $repeat, 2, 0, 0, 0, 0, 'bin'); return $gif->GetAnimation(); }
/** * Fit small image to specified bound * * @param string $src * @param string $dest * @param int $width * @param int $height * @return bool */ public function fit($src, $dest, $width, $height) { // Calculate $size = getimagesize($src); $ratio = max($width / $size[0], $height / $size[1]); $old_width = $size[0]; $old_height = $size[1]; $new_width = intval($old_width * $ratio); $new_height = intval($old_height * $ratio); // Resize @ini_set('memory_limit', apply_filters('image_memory_limit', WP_MAX_MEMORY_LIMIT)); $image = imagecreatefromstring(file_get_contents($src)); $new_image = wp_imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); if (IMAGETYPE_PNG == $size[2] && function_exists('imageistruecolor') && !imageistruecolor($image)) { imagetruecolortopalette($new_image, false, imagecolorstotal($image)); } // Destroy old image imagedestroy($image); // Save switch ($size[2]) { case IMAGETYPE_GIF: $result = imagegif($new_image, $dest); break; case IMAGETYPE_PNG: $result = imagepng($new_image, $dest); break; default: $result = imagejpeg($new_image, $dest); break; } imagedestroy($new_image); return $result; }
/** * Displays the image * * @param integer $quality image render quality 1-100, default 100 * @access public * @return void */ function render($quality = 100) { header('Content-type: image/' . $this->type); @imageinterlace($this->handle, 1); @imagegif($this->handle, NULL, $quality); @imagedestroy($this->handle); }
static function resizewidth($width, $imageold, $imagenew) { $image_info = getimagesize($imageold); $image_type = $image_info[2]; if ($image_type == IMAGETYPE_JPEG) { $image = imagecreatefromjpeg($imageold); } elseif ($this->image_type == IMAGETYPE_GIF) { $image = imagecreatefromgif($imageold); } elseif ($this->image_type == IMAGETYPE_PNG) { $image = imagecreatefrompng($imageold); } $ratio = imagesy($image) / imagesx($image); $height = $width * $ratio; //$width = imagesx($image) * $width/100; // $height = imagesx($image) * $width/100; $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $image_info[0], $image_info[1]); $image = $new_image; $compression = 75; $permissions = null; if ($image_type == IMAGETYPE_JPEG) { imagejpeg($image, $imagenew, $compression); } elseif ($image_type == IMAGETYPE_GIF) { imagegif($image, $imagenew); } elseif ($image_type == IMAGETYPE_PNG) { imagepng($image, $imagenew); } if ($permissions != null) { chmod($imagenew, $permissions); } }
function return_image($boolean) { if (function_exists('imagecreate')) { $im = imagecreate(1, 1); if ($boolean) { imagecolorallocate($im, 0, 255, 0); } else { imagecolorallocate($im, 255, 0, 0); } header("Content-type: image/gif"); imagegif($im); imagedestroy($im); } else { $grape_gif = ' R0lGODlhIAAgALMAAAAAAAAAgHCAkC6LV76+vvXeswD/ANzc3DLNMubm+v/6zS9PT6Ai8P8A//// /////yH5BAEAAAkALAAAAAAgACAAAAS00MlJq7046803AF3ofAYYfh8GIEvpoUZcmtOKAO5rLMva 0rYVKqX5IEq3XDAZo1GGiOhw5rtJc09cVGo7orYwYtYo3d4+DBxJWuSCAQ30+vNTGcxnOIARj3eT YhJDQ3woDGl7foNiKBV7aYeEkHEignKFkk4ciYaImJqbkZ+PjZUjaJOElKanqJyRrJyZgSKkokOs NYa2q7mcirC5I5FofsK6hcHHgsSgx4a9yzXK0rrV19gRADs= '; $php_rules = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl' . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr' . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r' . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg=='; if ($boolean) { $data = base64_decode($grape_gif); header("Content-type: image/gif"); } else { $data = base64_decode($php_rules); header("Content-type: image/png"); } echo $data; } }
/** * 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'; } }
public function resizeAndSaveImage($mimeType, $source_url, $destination_url) { // initial image buffer if ($mimeType == 'image/jpeg') { $image = imagecreatefromjpeg($source_url); } elseif ($mimeType == 'image/gif') { $image = imagecreatefromgif($source_url); } elseif ($mimeType == 'image/png') { $image = imagecreatefrompng($source_url); } // calc new image size keep aspect ratio $original_width = imagesx($image); $original_height = imagesy($image); // if current image is bigger then 500px, resize it to make it smaller $new_width = 500; if ($original_width > $new_width) { // calc new height by keep aspect ratio $new_height = $this->getResizeHeightByWidth($original_height, $original_width, $new_width); // resize with image buffer $new_image = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height); // saving image as full-alpha imagealphablending($new_image, false); imagesavealpha($new_image, true); if ($mimeType == 'image/jpeg') { imagejpeg($new_image, $source_url); } elseif ($mimeType == 'image/gif') { imagegif($new_image, $source_url); } elseif ($mimeType == 'image/png') { imagepng($new_image, $source_url); } imagedestroy($new_image); } imagedestroy($image); }
/** * Output an image. If the image is true-color, it will be converted * to a paletted image first using imagetruecolortopalette(). * * @param resource handle * @return bool */ public function output($handle) { if (imageistruecolor($handle)) { imagetruecolortopalette($handle, $this->dither, $this->ncolors); } return imagegif($handle); }
function resample() { // $filename = "$path.'/'.$filename"; $size = getimagesize($path . '/' . $filename); list($width, $height) = $size; $new_width = 100; $new_height = 100; $new_image = imagecreatetruecolor($new_width, $new_height); // 判断格式 if ($hz == 'jpg') { $image = imagecreatefromjpeg($path . '/' . $filename); } elseif ($hz == 'png') { $image = imagecreatefrompng($path . '/' . $filename); } else { $image = imagecreatefromgif($path . '/' . $filename); } // $image = imagecreatefromjpeg($filename); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // 判断格式 if ($hz == 'jpg') { imagejpeg($new_image, $path . '/' . $filename . 'R', 100); } elseif ($hz == 'png') { imagepng($new_image, $path . '/' . $filename . 'R', 100); } else { imagegif($new_image, $path . '/' . $filename . 'R', 100); } // imagejpeg($new_image, 'new.jpg', 100); imagedestroy($new_image); imagedestroy($image); }
function resize($width, $height) { list($w, $h) = getimagesize($_FILES['image']['tmp_name']); $ratio = max($width / $w, $height / $h); $h = ceil($height / $ratio); $x = ($w - $width / $ratio) / 2; $w = ceil($width / $ratio); $path = 'photos/' . $width . 'x' . $height . '_' . $_FILES['image']['name']; $imgString = file_get_contents($_FILES['image']['tmp_name']); $image = imagecreatefromstring($imgString); $tmp = imagecreatetruecolor($width, $height); imagecopyresampled($tmp, $image, 0, 0, $x, 0, $width, $height, $w, $h); /* Save image */ switch ($_FILES['image']['type']) { case 'image/jpeg': imagejpeg($tmp, $path, 100); break; case 'image/png': imagepng($tmp, $path, 0); break; case 'image/gif': imagegif($tmp, $path); break; default: exit; break; } return $path; /* cleanup memory */ imagedestroy($image); imagedestroy($tmp); }
function img_resize($src, $dest, $width, $height, $rgb = 0xffffff, $quality = 100) { if (!file_exists($src)) { return false; } $size = getimagesize($src); if ($size === false) { return false; } $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1)); $icfunc = 'imagecreatefrom' . $format; if (!function_exists($icfunc)) { return false; } $x_ratio = $width / $size[0]; $y_ratio = $height / $size[1]; if ($height == 0) { $y_ratio = $x_ratio; $height = $y_ratio * $size[1]; } elseif ($width == 0) { $x_ratio = $y_ratio; $width = $x_ratio * $size[0]; } $ratio = min($x_ratio, $y_ratio); $use_x_ratio = $x_ratio == $ratio; $new_width = $use_x_ratio ? $width : floor($size[0] * $ratio); $new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio); $new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2); $new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2); // если не нужно увеличивать маленькую картинку до указанного размера if ($size[0] < $new_width && $size[1] < $new_height) { $width = $new_width = $size[0]; $height = $new_height = $size[1]; } $isrc = $icfunc($src); $idest = imagecreatetruecolor($width, $height); imagefill($idest, 0, 0, $rgb); imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]); $i = strrpos($dest, '.'); if (!$i) { return ''; } $l = strlen($dest) - $i; $ext = substr($dest, $i + 1, $l); switch ($ext) { case 'jpeg': case 'jpg': imagejpeg($idest, $dest, $quality); break; case 'gif': imagegif($idest, $dest); break; case 'png': imagepng($idest, $dest); break; } imagedestroy($isrc); imagedestroy($idest); return true; }
function watermark($image, $mimeType, $imgWidth, $imgHeight, $watermark, $watermarkHeight, $watermarkWidth, $position = "center") { // Calculate the watermark position switch ($position) { case "center": $marginBottom = round($imgHeight / 2); $marginRight = round($imgWidth / 2) - round($watermarkWidth / 2); break; case "top-left": $marginBottom = round($imgHeight - $watermarkHeight); $marginRight = round($imgWidth - $watermarkWidth); break; case "bottom-left": $marginBottom = 5; $marginRight = round($imgWidth - $watermarkWidth); break; case "top-right": $marginBottom = round($imgHeight - $watermarkHeight); $marginRight = 5; break; default: $marginBottom = 2; $marginRight = 2; break; } $watermark = imagecreatefrompng($watermark); switch ($mimeType) { case "jpeg": case "jpg": $createImage = imagecreatefromjpeg($image); break; case "png": $createImage = imagecreatefrompng($image); break; case "gif": $createImage = imagecreatefromgif($image); break; default: $createImage = imagecreatefromjpeg($image); break; } $sx = imagesx($watermark); $sy = imagesy($watermark); imagecopy($createImage, $watermark, imagesx($createImage) - $sx - $marginRight, imagesy($createImage) - $sy - $marginBottom, 0, 0, imagesx($watermark), imagesy($watermark)); switch ($mimeType) { case "jpeg": case "jpg": imagejpeg($createImage, $image); break; case "png": imagepng($createImage, $image); break; case "gif": imagegif($createImage, $image); break; default: throw new \Exception("A watermark can only be applied to: jpeg, jpg, gif, png images "); break; } }
/** * 图片保存方法 * @param $fileName 保存路径 * @return void */ function save($fileName) { $this->saveCode(); imagegif($this->validate, $fileName); imagedestroy($this->validate); imagedestroy($this->image); }
public function save($sFileName) { $sExt = strtolower(array_pop(explode('.', $sFileName))); if (!$sExt) { return false; } if (!in_array($sExt, $this->aImageTypes)) { return false; } $bResult = false; switch ($sExt) { case 'jpg': case 'jpeg': $bResult = imagejpeg($this->rRes, $sFileName, $this->nJpegQuality); break; case 'gif': $bResult = imagegif($this->rRes, $sFileName); break; case 'png': imagealphablending($this->rRes, false); imagesavealpha($this->rRes, true); $bResult = imagepng($this->rRes, $sFileName); break; } if (!$bResult) { return false; } return true; }
/** * @desc ������� ������ ����������� * @return */ function outputPreview($szContent, $szMime) { $source = imagecreatefromstring($szContent); $nSourceHeight = imagesy($source); $nSourceWidth = imagesx($source); // �������� ������ � ������ if (DAO_IMAGE_THUMBNAIL_X == 0) { $szHeight = DAO_IMAGE_THUMBNAIL_Y; $szWidth = intval($nSourceWidth * DAO_IMAGE_THUMBNAIL_Y / $nSourceHeight); } elseif (DAO_IMAGE_THUMBNAIL_Y == 0) { $szWidth = DAO_IMAGE_THUMBNAIL_X; $szHeight = intval($nSourceHeight * DAO_IMAGE_THUMBNAIL_X / $nSourceWidth); } else { $szWidth = DAO_IMAGE_THUMBNAIL_X; $szHeight = DAO_IMAGE_THUMBNAIL_Y; } $thumb = imagecreatetruecolor($szWidth, $szHeight); imagecopyresized($thumb, $source, 0, 0, 0, 0, $szWidth, $szHeight, $nSourceWidth, $nSourceHeight); switch ($szMime) { case 'image/jpeg': imagejpeg($thumb); break; case 'image/png': imagepng($thumb); break; case 'image/gif': imagegif($thumb); break; default: imagejpeg($thumb); } }
function resize_image($src, $dest, $src_h, $src_w, $dest_h, $dest_w, $type, $src_x=0, $src_y=0) { $thumbnail_img = imagecreatetruecolor($dest_w, $dest_h); if ($type == 'gif') { $source = imagecreatefromgif($src); } else if ($type == 'jpg') { $source = imagecreatefromjpeg($src); } else { $source = imagecreatefrompng($src); } if ($src_x > 0 || $src_y > 0){ imagecopyresized($thumbnail_img, $source, 0, 0, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h); } else { imagecopyresampled($thumbnail_img, $source, $src_x, $src_y, 0, 0, $dest_w, $dest_h, $src_w, $src_h); } if ($type == 'gif') { imagegif($thumbnail_img, $dest); } else if ($type == 'jpg') { imagejpeg($thumbnail_img, $dest, 75); } else { imagepng($thumbnail_img, $dest, 7); } }
function resize($width, $height, $path, $i) { /* Get original image x y*/ list($w, $h) = getimagesize($_FILES['image']['tmp_name'][$i]); /* calculate new image size with ratio */ $ratio = max($width / $w, $height / $h); $h = ceil($height / $ratio); $x = ($w - $width / $ratio) / 2; $w = ceil($width / $ratio); /* new file name */ /* read binary data from image file */ $imgString = file_get_contents($_FILES['image']['tmp_name'][$i]); /* create image from string */ $image = imagecreatefromstring($imgString); $tmp = imagecreatetruecolor($width, $height); imagecopyresampled($tmp, $image, 0, 0, $x, 0, $width, $height, $w, $h); /* Save image */ switch ($_FILES['image']['type'][$i]) { case 'image/jpeg': imagejpeg($tmp, $path, 100); break; case 'image/png': imagepng($tmp, $path, 0); break; case 'image/gif': imagegif($tmp, $path); break; default: return false; break; } imagedestroy($image); imagedestroy($tmp); return true; }
private function __resizeGif($original, $new_filename, $scaled_width, $scaled_height, $width, $height) { $error = false; if (!($src = imagecreatefromgif($original))) { $this->__errors[] = 'There was an error creating your resized image (gif).'; $error = true; } if (!($tmp = imagecreatetruecolor($scaled_width, $scaled_height))) { $this->__errors[] = 'There was an error creating your true color image (gif).'; $error = true; } if (!imagecopyresampled($tmp, $src, 0, 0, 0, 0, $scaled_width, $scaled_height, $width, $height)) { $this->__errors[] = 'There was an error creating your true color image (gif).'; $error = true; } if (!($new_image = imagegif($tmp, $new_filename))) { $this->__errors[] = 'There was an error writing your image to file (gif).'; $error = true; } imagedestroy($tmp); if (false == $error) { return $new_image; } return false; }
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 GetImageGif($file, $width, $maxWidth, $maxHeight) { header("Content-type: image/gif"); $size = getimagesize($file); /* Propiedades de la imagen */ if ($width == -1) { $height = $size[1]; $width = $size[0]; } else { $porc = $width * 100 / $size[0]; // Calculo el porcentaje en que se reduce la imagen.. $height = $porc * $size[1] / 100; // Calculo el alto de la nueva imagen.. } if ($maxWidth != -1) // Si la imagen tiene que tener un ancho máximo.. if ($width > $maxWidth) { // Si el ancho es mayor al ancho máximo.. $height = $maxWidth * $height / $width; // Calculo el alto proporcional al ancho máximo.. $width = $maxWidth; // Le asigno el ancho máximo.. } if ($maxHeight != -1) // Si la imagen tiene que tener un alto máximo.. if ($height > $maxHeight) { // Si el alto es mayor al ancho máximo.. $width = $maxHeight * $width / $height; // Calculo el alto proporcional al ancho máximo.. $height = $maxHeight; // Le asigno el ancho máximo.. } $im = imagecreatefromgif($file); /* Tomo la imagen de origen */ $img = imagecreatetruecolor($width, $height); /* [0] Nuevo ancho, [1] ALTO, me creo un CANVAS, algo similar que en Firework */ // imagecopyresized($img, $im, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); /* Copio en mi CANVAS la imagen $im en la dimensión que deseo */ imagecopyresampled($img, $im, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); imagegif($img); /* Exporto la CANVAS a GIF */ imagedestroy($img); /* La Borro de la Cache */ }
public function resize($filename, $dst, $dst_w, $dst_h) { list($width, $height) = getimagesize($filename); $center_width = $width > $height ? $dst_w * $height / $dst_h : $width; $center_height = $height > $width ? $dst_h * $width / $dst_w : $height; $src_x = $width > $height ? ($width - $center_width) / 2 : 0; $src_y = $height > $width ? ($height - $center_height) / 2 : 0; $thumb = imagecreatetruecolor($dst_w, $dst_h); $type = mime_content_type($filename); switch (substr($type, 6)) { case 'jpeg': $source = imagecreatefromjpeg($filename); imagecopyresampled($thumb, $source, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $center_width, $center_height); imagejpeg($thumb, $dst); break; case 'gif': $source = imagecreatefromgif($filename); imagecopyresampled($thumb, $source, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $center_width, $center_height); imagegif($thumb, $dst); break; case 'png': $source = imagecreatefrompng($filename); imagecopyresampled($thumb, $source, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $center_width, $center_height); imagepng($thumb, $dst); break; } imagedestroy($thumb); }
public function genFile($fileName, $quality = 100) { $image = parent::returnThumbnail(); $result = false; if (!empty($image)) { switch ($this->image_type) { case 1: $result = imagegif($image, $fileName); break; case 2: $quality = (int) $quality; if ($quality < 0 || $quality > 100) { $quality = 75; } // end if $result = imagejpeg($image, $fileName, $quality); break; case 3: $result = imagepng($image, $fileName); break; case 15: $result = imagewbmp($image, $fileName); break; } // end switch } return $result; }
/** * Render the gradient to an image **/ public function render($width, $height, $format = 'png', $filename = null) { $image = $this->render_gd($width, $height); switch ($format) { case 'jpg': case 'jpeg': case 'jpe': if (empty($filename)) { header('Content-Type: image/jpeg'); } imagejpeg($image, $filename, 90); break; case 'gif': if (empty($filename)) { header('Content-Type: image/gif'); } imagegif($image, $filename); break; case 'png': default: if (empty($filename)) { header('Content-Type: image/png'); } imagepng($image, $filename, 9); break; } imagedestroy($image); }
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 create_pic($upfile, $new_path, $width) { $quality = 100; $image_path = $upfile; $image_info = getimagesize($image_path); $exname = ''; //1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF switch ($image_info[2]) { case 1: @($image = imagecreatefromgif($image_path)); $exname = 'gif'; break; case 2: @($image = imagecreatefromjpeg($image_path)); $exname = 'jpg'; break; case 3: @($image = imagecreatefrompng($image_path)); $exname = 'png'; break; case 6: @($image = imagecreatefromwbmp($image_path)); $exname = 'wbmp'; break; } $T_width = $image_info[0]; $T_height = $image_info[1]; if (!empty($image)) { $image_x = imagesx($image); $image_y = imagesy($image); } else { return FALSE; } @chmod($new_path, 0777); if ($image_x > $width) { $x = $width; $y = intval($x * $image_y / $image_x); } else { @copy($image_path, $new_path . '.' . $exname); return $exname; } $newimage = imagecreatetruecolor($x, $y); imagecopyresampled($newimage, $image, 0, 0, 0, 0, $x, $y, $image_x, $image_y); switch ($image_info[2]) { case 1: imagegif($newimage, $new_path . '.gif', $quality); break; case 2: imagejpeg($newimage, $new_path . '.jpg', $quality); break; case 3: imagepng($newimage, $new_path . '.png', $quality); break; case 6: imagewbmp($newimage, $new_path . '.wbmp', $quality); break; } imagedestroy($newimage); return $exname; }
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); }
private static function __image() { self::__background(); self::__adulterate(); self::__font(); header("Expires: 0" . PHP_EOL); header("Cache-Control: no-cache" . PHP_EOL); header("Pragma: no-cache" . PHP_EOL); if (function_exists('imagejpeg')) { header('Content-type:image/jpeg' . PHP_EOL); $void = imagejpeg(self::$im); } else { if (function_exists('imagepng')) { header('Content-type:image/png' . PHP_EOL); $void = imagepng(self::$im); } else { if (function_exists('imagegif')) { header('Content-type:image/gif' . PHP_EOL); $void = imagegif(self::$im); } else { return false; } } } imagedestroy(self::$im); return $void; }
/** * Save the image as the image type the original image was * * @param string $savePath - The path to store the new image * @param string $imageQuality - The qulaity level of image to create * * @param bool $download * * @return null Saves the image */ public function saveImage($savePath, $imageQuality = "100", $download = false) { if (!$this->newImage) { return; } switch ($this->ext) { case 'image/jpg': case 'image/jpeg': // Check PHP supports this file type if (imagetypes() & IMG_JPG) { imagejpeg($this->newImage, $savePath, $imageQuality); } break; case 'image/gif': // Check PHP supports this file type if (imagetypes() & IMG_GIF) { imagegif($this->newImage, $savePath); } break; case 'image/png': $invertScaleQuality = 9 - round($imageQuality / 100 * 9); // Check PHP supports this file type if (imagetypes() & IMG_PNG) { imagepng($this->newImage, $savePath, $invertScaleQuality); } break; } if ($download) { header('Content-Description: File Transfer'); header("Content-type: application/octet-stream"); header("Content-disposition: attachment; filename= " . $savePath . ""); readfile($savePath); } imagedestroy($this->newImage); }
function mkthumb($img_src, $img_width = "100", $img_height = "100", $folder_scr = "include/files", $des_src = "include/files") { // Größe und Typ ermitteln list($src_width, $src_height, $src_typ) = getimagesize($folder_scr . "/" . $img_src); if (!$src_typ) { return false; } // calculate new size if ($src_width >= $src_height) { $new_image_height = $src_height / $src_width * $img_width; $new_image_width = $img_width; if ($new_image_height > $img_height) { $new_image_width = $new_image_width / $new_image_height * $img_height; $new_image_height = $img_height; } } else { $new_image_width = $src_width / $src_height * $img_height; $new_image_height = $img_height; if ($new_image_width > $img_width) { $new_image_height = $new_image_height / $new_image_width * $img_width; $new_image_width = $img_width; } } // for the case that the thumbnail would be bigger then the original picture if ($new_image_height > $src_height) { $new_image_width = $new_image_width * $src_height / $new_image_height; $new_image_height = $src_height; } if ($new_image_width > $src_width) { $new_image_height = $new_image_height * $new_image_width / $src_width; $new_image_width = $src_width; } if ($src_typ == 1) { $image = imagecreatefromgif($folder_scr . "/" . $img_src); $new_image = imagecreate($new_image_width, $new_image_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $src_width, $src_height); imagegif($new_image, $des_src . "/" . $img_src . "_thumb", 100); imagedestroy($image); imagedestroy($new_image); return true; } elseif ($src_typ == 2) { $image = imagecreatefromjpeg($folder_scr . "/" . $img_src); $new_image = imagecreatetruecolor($new_image_width, $new_image_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $src_width, $src_height); imagejpeg($new_image, $des_src . "/" . $img_src . "_thumb", 100); imagedestroy($image); imagedestroy($new_image); return true; } elseif ($src_typ == 3) { $image = imagecreatefrompng($folder_scr . "/" . $img_src); $new_image = imagecreatetruecolor($new_image_width, $new_image_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $src_width, $src_height); imagepng($new_image, $des_src . "/" . $img_src . "_thumb"); imagedestroy($image); imagedestroy($new_image); return true; } else { return false; } }
function JPGText($str, $fontname, $fontsize, $backcol, $txtcol) { global $layout; Header("Last-Modified: " . gmDate("D, d M Y H:i:s", Time()) . " GMT"); Header("Expires: " . gmDate("D, d M Y H:i:s", Time() - 3601) . " GMT"); Header("Pragma: no-cache"); Header("Cache-control: no-cache"); Header("Content-Type: image/jpeg"); $a = ImageTTFBBox($fontsize, 0, $fontname, $str); $width = $a[2] + 4; $bla = get_maximum_height($fontname, $fontsize); $height = $bla[0] + 3; $bl = $bla[1]; $im = ImageCreate($width, $height); $bgcol = ImageColorAllocate($im, $backcol['red'], $backcol['green'], $backcol['blue']); $fgcol = ImageColorAllocate($im, $txtcol['red'], $txtcol['green'], $txtcol['blue']); if (!function_exists(imagegif)) { imageTTFText($im, $fontsize, 0, 2, $bl + $fontsize / 6 + 2, $fgcol, $fontname, $str); imagejpeg($im, "", 80); } else { ImageColorTransparent($im, $bgcol); imageTTFText($im, $fontsize, 0, 2, $bl + $fontsize / 6 + 2, $fgcol, $fontname, $str); imagegif($im); } ImageDestroy($im); }