static function newGalleryImage($image) { # Grab filename and image type from upload $file_ext = self::$imageTypes[$image['type']]; $filename = $image['name']; # Upload of image copy($image['tmp_name'], "gallery_images/" . $filename); # Grabs file suffix for variable function calls $function_suffix = strtoupper($file_ext); # Variable read/write functions for image creation $function_to_read = 'ImageCreateFrom' . $function_suffix; $function_to_write = 'Image' . $function_suffix; # Determine the file size and create a proportionally sized thumbnail $size = GetImageSize("gallery_images/" . $filename); if ($size[0] > $size[1]) { $thumbnail_width = 200; $thumbnail_height = (int) (200 * $size[1] / $size[0]); } else { $thumbnail_width = (int) (200 * $size[0] / $size[1]); $thumbnail_height = 200; } $source_handle = $function_to_read("gallery_images/" . $filename); if ($source_handle) { $destination_handle = ImageCreateTrueColor($thumbnail_width, $thumbnail_height); ImageCopyResampled($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]); } $function_to_write($destination_handle, "gallery_images/tb/" . $filename); }
/** * 把图片生成缩略图1 * @param string $srcFile 源文件 * @param string $dstFile 目标文件 * @param int $dstW 目标图片宽度 * @param int $dstH 目标文件高度 * @param string $dstFormat 目标文件生成的格式, 有png和jpg两种格式 * @return 错误返回错误对象 */ public static function makeThumb1($srcFile, $dstFile, $dstW, $dstH, $dstFormat = "png") { //打开图片 $data = GetImageSize($srcFile, &$info); switch ($data[2]) { case 1: $im = @ImageCreateFromGIF($srcFile); break; case 2: $im = @imagecreatefromjpeg($srcFile); break; case 3: $im = @ImageCreateFromPNG($srcFile); break; } if (!$im) { throw new TM_Exception(__CLASS__ . ": Create image failed"); } //设定图片大小 $srcW = ImageSX($im); $srcH = ImageSY($im); $ni = ImageCreate($dstW, $dstH); ImageCopyResized($ni, $im, 0, 0, 0, 0, $dstW, $dstH, $srcW, $srcH); //生成指定格式的图片 if ($dstFormat == "png") { imagepng($ni, $dstFile); } elseif ($dstFormat == "jpg") { ImageJpeg($ni, $dstFile); } else { imagepng($ni, $dstFile); } }
function resizeImg($origPath, $mW, $mH) { // Read the size $rst['size'] = GetImageSize($origPath); $w = $rst['size'][0]; $h = $rst['size'][1]; // Proportionally resize the image to the max sizes specified above $xRatio = $mW / $w; $yRatio = $mH / $h; if ($w <= $mW && $h <= $mH) { $tnW = $w; $tnH = $h; } elseif ($xRatio * $h < $mH) { $tnH = ceil($xRatio * $h); $tnW = $mW; } else { $tnW = ceil($yRatio * $w); $tnH = $mH; } // Create the new image! if ($rst['size']['mime'] == 'image/jpg' || $rst['size']['mime'] == 'image/jpeg') { $rst['src'] = imagecreatefromjpeg($origPath); $rst['dst'] = ImageCreateTrueColor($tnW, $tnH); $rst['resize'] = ImageCopyResized($rst['dst'], $rst['src'], 0, 0, 0, 0, $tnW, $tnH, $w, $h); $rst['imgMod'] = imagejpeg($rst['dst'], $origPath, 100); } else { $rst['note'] = 'Error with file type!'; return false; } return $rst; }
function ThumbnailImage($path, $maxdimension = 100) { $this->maxdimension = $maxdimension; //check path is_file($path) or die("File: {$path} doesn't exist."); //check type $extension = substr($path, strpos($path, ".") + 1); $extension = strtolower($extension); in_array($extension, $this->types) or die("Incorrect file type."); $this->fileextension = $extension; $this->setMimeType($extension); //get dimensions by creating imageproperties $this->imageproperties = GetImageSize($path); //create image if ($extension == "jpeg" || $extension == "jpg") { $this->image = imagecreatefromJPEG($path); } elseif ($extension == "gif") { $this->image = imagecreatefromGIF($path); } elseif ($extension == "png") { $this->image = imagecreatefromPNG($path); } else { die("Couldn't create image."); } $this->createThumb(); }
function upload_image_and_thumbnail($fileData, $size, $subFolder, $prefix) { if (strlen($fileData['name']) > 4) { $error = 0; $destFolder = WWW_ROOT . $subFolder; $realFileName = $fileData['name']; if (!is_dir($destFolder)) { mkdir($destFolder, true); } $filetype = $this->getFileExtension($fileData['name']); $filetype = strtolower($filetype); if (!in_array($fileData['type'], $this->contentType)) { return false; exit; } else { if ($fileData['size'] > 700000) { return false; exit; } else { $imgsize = GetImageSize($fileData['tmp_name']); } } if (is_uploaded_file($fileData['tmp_name'])) { if (!copy($fileData['tmp_name'], $destFolder . '/' . $realFileName)) { return false; exit; } else { $this->resize_img($destFolder . '/' . $realFileName, $size, $destFolder . '/' . $prefix . $realFileName); unlink($destFolder . '/' . $realFileName); } } return $fileData; } }
/** * Parses image-replace properties * @access public * @param $url * @return string */ public function image_replace($value) { $url = preg_match('/ (?:url\\(\\s*)? # maybe url( [\'"]? # maybe quote ([^\'\\"\\)]*) # 1 = URI [\'"]? # maybe end quote (?:\\s*\\))? # maybe ) /xs', $value, $match); if ($match) { $url = $match[1]; // Get the size of the image file $size = GetImageSize($this->source->find($url)); $width = $size[0]; $height = $size[1]; // Make sure theres a value so it doesn't break the css if (!$width && !$height) { $width = $height = 0; } // Build the selector $properties = 'background:url(' . $url . ') no-repeat 0 0;height:0;padding-top:' . $height . 'px;width:' . $width . 'px;display:block;text-indent:-9999px;overflow:hidden;'; return $properties; } return false; }
function getSize($pic) { global $width, $height; $size = GetImageSize($pic); $width = $size[0]; $height = $size[1]; }
public function loadFile($thumbnail, $image) { $imgData = @GetImageSize($image); if (!$imgData) { throw new Exception(sprintf('Could not load image %s', $image)); } if (in_array($imgData['mime'], $this->imgTypes)) { $loader = $this->imgLoaders[$imgData['mime']]; if (!function_exists($loader)) { throw new Exception(sprintf('Function %s not available. Please enable the GD extension.', $loader)); } $this->source = $loader($image); $this->sourceWidth = $imgData[0]; $this->sourceHeight = $imgData[1]; $this->sourceMime = $imgData['mime']; $thumbnail->initThumb($this->sourceWidth, $this->sourceHeight, $this->maxWidth, $this->maxHeight, $this->scale, $this->inflate); $this->thumb = imagecreatetruecolor($thumbnail->getThumbWidth(), $thumbnail->getThumbHeight()); if ($imgData[0] == $this->maxWidth && $imgData[1] == $this->maxHeight) { $this->thumb = $this->source; } else { imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $thumbnail->getThumbWidth(), $thumbnail->getThumbHeight(), $imgData[0], $imgData[1]); } return true; } else { throw new Exception(sprintf('Image MIME type %s not supported', $imgData['mime'])); } }
public static function getImageType($srcFile) { $data = @GetImageSize($srcFile); if ($data === false) { return false; } else { switch ($data[2]) { case 1: return 'gif'; break; case 2: return 'jpg'; break; case 3: return 'png'; break; case 4: return 'swf'; break; case 5: return 'psd'; break; case 6: return 'bmp'; break; case 7: return 'tiff'; break; case 8: return 'tiff'; break; case 9: return 'jpc'; break; case 10: return 'jp2'; break; case 11: return 'jpx'; break; case 12: return 'jb2'; break; case 13: return 'swc'; break; case 14: return 'iff'; break; case 15: return 'wbmp'; break; case 16: return 'xbm'; break; default: return false; } } }
/** * Construction for class * * @param string $sourceFolder : end with slash '/' * @param string $sourceName * @param string $destFolder : end with slash '/' * @param string $destName * @param int $maxWidth * @param int $maxHeight * @param string $cropRatio * @param int $quality * @param string $color * * @return ImageResizer */ public function __construct($sourceFolder, $sourceName, $destFolder, $destName, $maxWidth = 0, $maxHeight = 0, $cropRatio = '', $quality = 90, $color = 'ffffff') { $this->sourceFolder = $sourceFolder; $this->sourceName = $sourceName; $this->destFolder = $destFolder; $this->destName = $destName; $this->maxWidth = $maxWidth; $this->maxHeight = $maxHeight; $this->cropRatio = $cropRatio; $this->quality = $quality; $this->color = $color; if (!file_exists($this->sourceFolder . $this->sourceName)) { echo 'Error: image does not exist: ' . $this->sourceFolder . $this->sourceName; return null; } $size = GetImageSize($this->sourceFolder . $this->sourceName); $mime = $size['mime']; // Make sure that the requested file is actually an image if (substr($mime, 0, 6) != 'image/') { echo 'Error: requested file is not an accepted type: ' . $this->sourceFolder . $this->sourceName; return null; } $this->size = $size; if ($color != '') { $this->color = preg_replace('/[^0-9a-fA-F]/', '', $color); } else { $this->color = false; } }
/** * The second last process, should only be getting everything * syntaxically correct, rather than doing any heavy processing * * @author Anthony Short * @return $css string */ public static function post_process() { if ($found = CSS::find_properties_with_value('image-replace', 'url\\([\'\\"]?([^)]+)[\'\\"]?\\)')) { foreach ($found[4] as $key => $value) { $path = $url = str_replace("\\", "/", unquote($value)); # If they're getting an absolute file if ($path[0] == "/") { $path = DOCROOT . ltrim($path, "/"); } # Check if it exists if (!file_exists($path)) { FB::log("ImageReplace - Image doesn't exist " . $path); } # Make sure it's an image if (!is_image($path)) { FB::log("ImageReplace - File is not an image: {$path}"); } // Get the size of the image file $size = GetImageSize($path); $width = $size[0]; $height = $size[1]; // Make sure theres a value so it doesn't break the css if (!$width && !$height) { $width = $height = 0; } // Build the selector $properties = "\n\t\t\t\t\tbackground:url({$url}) no-repeat 0 0;\n\t\t\t\t\theight:{$height}px;\n\t\t\t\t\twidth:{$width}px;\n\t\t\t\t\tdisplay:block;\n\t\t\t\t\ttext-indent:-9999px;\n\t\t\t\t\toverflow:hidden;\n\t\t\t\t"; CSS::replace($found[2][$key], $properties); } # Remove any left overs CSS::replace($found[1], ''); } }
private function resizeImage($file, $data, $tmd = 600, $quality = 100) { $data['type'] = "image/jpeg"; $basename = basename($file); $filesDir = $this->input->post('uploadDir'); // хэш нередактируемой карты! $uploaddir = implode(array($this->input->server('DOCUMENT_ROOT'), 'storage', $tmd, $filesDir), DIRECTORY_SEPARATOR); $srcFile = implode(array($this->input->server('DOCUMENT_ROOT'), 'storage', 'source', $filesDir, $basename), DIRECTORY_SEPARATOR); $image = $this->createimageByType($data, $srcFile); if (!file_exists($uploaddir)) { mkdir($uploaddir, 0775, true); } $size = GetImageSize($srcFile); $new = ImageCreateTrueColor($size['1'], $size['0']); if ($size['1'] > $tmd || $size['0'] > $tmd) { if ($size['1'] < $size['0']) { $hNew = round($tmd * $size['1'] / $size['0']); $new = ImageCreateTrueColor($tmd, $hNew); ImageCopyResampled($new, $image, 0, 0, 0, 0, $tmd, $hNew, $size['0'], $size['1']); } if ($size['1'] >= $size['0']) { $hNew = round($tmd * $size['0'] / $size['1']); $new = ImageCreateTrueColor($hNew, $tmd); ImageCopyResampled($new, $image, 0, 0, 0, 0, $hNew, $tmd, $size['0'], $size['1']); } } //print $uploaddir."/".TMD."/".$filename.".jpg<br>"; imageJpeg($new, $uploaddir . DIRECTORY_SEPARATOR . $basename, $quality); //header("content-type: image/jpeg");// активировать для отладки //imageJpeg ($new, "", 100);//активировать для отладки imageDestroy($new); }
/** * Fetches gallery info for the specified gallery and immediate children. */ function get_gallery($gallery_name, $getChildGalleries = 1) { $gal = new gallery($gallery_name); //if fail then try to open generic metadata $fp = @fopen($this->config->base_path . $this->config->pathto_galleries . $gallery_name . "/metadata.csv", "r"); if ($fp) { while ($temp[] = fgetcsv($fp, 2048)) { } fclose($fp); list($gal->filename, $gal->name, $gal->desc, $gal->long_desc, $gal->date) = $temp[1]; for ($i = 0; $i < count($temp) - 3; $i++) { $gal->images[$i] = new image(); list($gal->images[$i]->filename, $gal->images[$i]->name, $gal->images[$i]->desc, $gal->images[$i]->long_desc, $gal->images[$i]->date, $gal->images[$i]->sort) = $temp[$i + 2]; $gal->images[$i]->full_path = $gal->name . "/" . $gal->images[$i]->filename; //get image size and type list($gal->images[$i]->width, $gal->images[$i]->height, $gal->images[$i]->type) = substr($gal->images[$i]->filename, 0, 7) == "http://" ? @GetImageSize($gal->images[$i]->filename) : @GetImageSize($this->config->base_path . $this->config->pathto_galleries . $gal->id . "/" . $gal->images[$i]->filename); } //discover child galleries $dir = photostack::get_listing($this->config->base_path . $this->config->pathto_galleries . $gallery_name . "/", "dirs"); if ($getChildGalleries) { //but only fetch their info if required too foreach ($dir->dirs as $gallery) { $gal->galleries[] = $this->get_gallery($gallery_name . "/" . $gallery, $getChildGalleries); } } else { //otherwise just copy their names in so they can be counted $gal->galleries = $dir->dirs; } return $gal; } else { return parent::get_gallery($gallery_name, $getChildGalleries); } }
function validate_image($data, $size = '', $hlimit = '', $vlimit = '') { $exts = array('gif', 'jpg', 'jpeg', 'png'); $fname = $data['name']; $tmpname = $data['tmp_name']; $ext = preg_replace('/.*\\./', '', $fname); $res = ''; // Check the image extension $res = 0; foreach ($exts as $key) { $res |= preg_match('/^' . $key . '$/', $ext); } if ($res) { $image_size = GetImageSize($tmpname); $w = $image_size[0]; $h = $image_size[1]; $r = validate_image_cmpiterator($data['size'], $size, 'Размер', 1, 'байт'); if ($r != '') { return $r; } $r = validate_image_cmpiterator($w, $hlimit, 'Ширина', 0, 'пикселей'); if ($r != '') { return $r; } $r = validate_image_cmpiterator($h, $vlimit, 'Высота', 0, 'пикселей'); if ($r != '') { return $r; } return ''; } else { return 'Поддерживаются только следующие расширения файлов: gif, jpg, jpeg, png.'; } }
function login($type = "") { $this->obj->db->where('app_users_list.username', $this->obj->input->post('email')); $this->obj->db->where('password', $this->_prep_password($this->obj->input->post('password'))); $this->obj->db->where('status_active', '1'); $this->obj->db->join('app_users_profile', 'app_users_profile.id=app_users_list.id', 'right'); $this->obj->db->limit(1); $Q = $this->obj->db->get($this->table); if ($Q->num_rows() > 0) { $user = $Q->row(); $this->_start_session($user); $this->obj->db->where('id', $this->obj->session->userdata('id')); $p = $this->obj->db->get($this->table_profile); $profile = $p->row(); if ($profile->avatar != "" && @GetImageSize($profile->avatar)) { $profile->avatar = $profile->avatar; } else { $profile->avatar = base_url() . "media/images/profile.jpeg"; } $profile = array('avatar' => $profile->avatar, 'name_display' => $profile->name_display); $this->obj->session->set_userdata($profile); $this->obj->db->update('app_users_list', array('online' => 1, 'last_login' => time()), array('id' => $user->id)); $this->_log($type . ' Login successful...'); $this->obj->session->set_flashdata('notification', 'Login successful...'); return true; } else { $this->_destroy_session(); $this->_log($type . ' Login failed...'); $this->obj->session->set_flashdata('notification', 'Login failed...'); return false; } }
function SelectRandomImage($dirname = '.', $portrait = true, $landscape = true, $square = true) { // return a random image filename from $dirname // the last 3 parameters determine what aspect ratio of images // may be returned $possibleimages = array(); if ($dh = opendir($dirname)) { while ($file = readdir($dh)) { if (is_file($dirname . '/' . $file) && preg_match('#\\.(jpg|jpeg|gif|png|tiff|bmp)$#i', $file)) { if ($gis = @GetImageSize($dirname . '/' . $file)) { if ($portrait && $gis[0] < $gis[1]) { // portrait $possibleimages[] = $file; } elseif ($landscape && $gis[0] > $gis[1]) { // landscape $possibleimages[] = $file; } elseif ($square) { // square $possibleimages[] = $file; } } } } closedir($dh); } if (empty($possibleimages)) { return false; } if (phpversion() < '4.2.0') { mt_srand(time()); } $randkey = mt_rand(0, count($possibleimages) - 1); return realpath($dirname . '/' . $possibleimages[$randkey]); }
function SetVar($srcFile, $echoType) { if (!file_exists($srcFile)) { echo '源图片文件不存在!'; exit; } $this->srcFile = $srcFile; $this->echoType = $echoType; $info = ""; $data = GetImageSize($this->srcFile, $info); switch ($data[2]) { case 1: if (!function_exists("imagecreatefromgif")) { echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>"; exit; } $this->im = ImageCreateFromGIF($this->srcFile); break; case 2: if (!function_exists("imagecreatefromjpeg")) { echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>"; exit; } $this->im = ImageCreateFromJpeg($this->srcFile); break; case 3: $this->im = ImageCreateFromPNG($this->srcFile); break; } $this->srcW = ImageSX($this->im); $this->srcH = ImageSY($this->im); }
/** * @group chart */ public function testTimeline() { print "\n" . __METHOD__ . ' '; $this->_rootLogin(); $name_img = 'img_tmp.png'; if (file_exists($name_img)) { unlink($name_img); } $this->assertTrue(function_exists("imagepng"), "(imagepng not found)"); $this->dispatch('chart/timeline/datetimeline/' . date("Y-m-d", time())); $img = $this->response->outputBody(); $this->_isLogged($img); $this->assertModule('default'); $this->assertController('chart'); $this->assertAction('timeline'); if (empty($img)) { $this->assertTrue(FALSE, "image is empty!"); } $f = fopen($name_img, 'w'); $res = fwrite($f, $img); if (!$res) { $this->assertTrue(FALSE, "file {$name_img} can't writing!"); } fclose($f); $this->assertNotNull($size = GetImageSize($name_img)); $this->assertGreaterThan(700, $size[0]); // width $this->assertGreaterThan(400, $size[1]); // height unlink($name_img); $this->assertFileNotExists($name_img, "file {$name_img} not deleted!"); }
public function resize_image($data, $imgX, $sizedef, $lid, $imgid) { $file = $data["raw_name"]; $type = $data["file_ext"]; $outfile = $imgX[$sizedef]['dir'] . "/" . $lid . "/" . $file . '.jpg'; $path = $this->config->item("upload_dir"); $image = $this->create_image_container($file, $type); if ($image) { $size = GetImageSize($path . $file . $type); $old = $image; // сей форк - не просто так. непонятно, правда, почему... if ($size['1'] < $size['0']) { $h_new = round($imgX[$sizedef]['max_dim'] * ($size['1'] / $size['0'])); $measures = array($imgX[$sizedef]['max_dim'], $h_new); } if ($size['1'] >= $size['0']) { $h_new = round($imgX[$sizedef]['max_dim'] * ($size['0'] / $size['1'])); $measures = array($h_new, $imgX[$sizedef]['max_dim']); } $new = ImageCreateTrueColor($measures[0], $measures[1]); ImageCopyResampled($new, $image, 0, 0, 0, 0, $measures[0], $measures[1], $size['0'], $size['1']); imageJpeg($new, $outfile, $imgX[$sizedef]['quality']); $this->db->query("UPDATE `images` SET `images`.`" . $sizedef . "` = ? WHERE `images`.`id` = ?", array(implode($measures, ","), $imgid)); imageDestroy($new); } }
function SetVar($srcFile, $echoType) { $this->srcFile = $srcFile; $this->echoType = $echoType; $info = ''; $data = GetImageSize($this->srcFile, $info); switch ($data[2]) { case 1: if (!function_exists('imagecreatefromgif')) { exit; } $this->im = ImageCreateFromGIF($this->srcFile); break; case 2: if (!function_exists('imagecreatefromjpeg')) { exit; } $this->im = ImageCreateFromJpeg($this->srcFile); break; case 3: $this->im = ImageCreateFromPNG($this->srcFile); break; } $this->srcW = ImageSX($this->im); $this->srcH = ImageSY($this->im); }
function Thumb($img, $width = '', $height = '') { $thumb_img = 'Public/thumb.jpg'; if ($img) { $img = substr($img, 1); $is_img = GetImageSize('./' . $img); //设置宽高 生成缩略图 if (!empty($width) && !empty($height)) { import('ORG.Util.Image'); $Image = new Image(); $filename = 'Uploads/thumb/' . $width . '_' . $height . '/'; //缩略图存储路径 $new_name = strtr($img, array('/' => '_')); if (!file_exists($filename)) { @mkdir($filename, 0755); } if ($is_img) { $is_thumb = GetImageSize('./' . $filename . $new_name); if ($is_thumb) { $thumb_img = $filename . $new_name; } else { $Image->thumb($img, $filename . $new_name, '', $width, $height); $thumb_img = $filename . $new_name; } } } else { if ($is_img) { $thumb_img = $img; } } } return '/' . $thumb_img; }
function scan_smilie_dir() { $smilies = ''; chdir("./img/smilies"); $hnd = opendir("."); while ($file = readdir($hnd)) { if (is_file($file)) { if ($file != "." && $file != "..") { if (ereg(".gif|.jpg|.png|.jpeg", $file)) { $smilie_list[] = $file; } } } } closedir($hnd); if (isset($smilie_list)) { asort($smilie_list); for ($i = 0; $i < sizeof($smilie_list); $i++) { $size = GetImageSize($smilie_list[$i]); if (is_array($size)) { $smilies[$smilie_list[$i]] = "<img src=\"img/smilies/{$smilie_list[$i]}\" {$size['3']}>"; } } } chdir("../../"); return $smilies; }
function getImageData($data) { $size = GetImageSize($this->source); switch ($data) { case 'width': return $size[0]; break; case 'height': return $size[1]; break; case 'type': switch ($size[2]) { case 1: return 'gif'; break; case 2: return 'jpg'; break; case 3: return 'png'; break; } break; } }
/** * Construction for class * * @param string $sourceFolder : end with slash '/' * @param string $sourceName * @param string $destFolder : end with slash '/' * @param string $destName * @param int $maxWidth * @param int $maxHeight * @param string $cropRatio * @param int $quality * @param string $color * * @return ImageResizer */ function __construct($sourceFolder, $sourceName, $destFolder, $destName, $maxWidth = 0, $maxHeight = 0, $cropRatio = '', $quality = 90, $color = '') { $this->sourceFolder = $sourceFolder; $this->sourceName = $sourceName; $this->destFolder = $destFolder; $this->destName = $destName; $this->maxWidth = $maxWidth; $this->maxHeight = $maxHeight; $this->cropRatio = $cropRatio; $this->quality = $quality; $this->color = $color; $klog = new KLogger(Yii::getPathOfAlias('common.log') . DIRECTORY_SEPARATOR . 'resize_image_log', KLogger::INFO); if (!file_exists($this->sourceFolder . $this->sourceName)) { echo 'Error: image does not exist: ' . $this->sourceFolder . $this->sourceName; $this->file_error = true; $klog->LogInfo('Error: image does not exist: ' . $this->sourceFolder . $this->sourceName); return null; } $size = GetImageSize($this->sourceFolder . $this->sourceName); $mime = $size['mime']; // Make sure that the requested file is actually an image if (substr($mime, 0, 6) != 'image/') { echo 'Error: requested file is not an accepted type: ' . $this->sourceFolder . $this->sourceName; $klog->LogInfo('Error: requested file is not an accepted type: ' . $this->sourceFolder . $this->sourceName); $this->file_error = true; return null; } $this->size = $size; if ($color != '') { $this->color = preg_replace('/[^0-9a-fA-F]/', '', $color); } else { $this->color = FALSE; } }
protected function afterUpload($fileName) { global $PRJ_DIR; $fileName = $PRJ_DIR . $fileName; $i = @GetImageSize($fileName); $old_img_width = $i[0]; $old_img_height = $i[1]; $resize = false; if (isset($this->props['sizes'])) { $asizes = explode(',', $this->props['sizes']); foreach ($asizes as $sz) { $img_width = $i[0]; $img_height = $i[1]; $asz = explode('|', $sz); if (sizeof($asz) == 2) { $asizes2 = explode('x', $asz[1]); $max_width = $asizes2[0]; $max_height = $asizes2[1]; if ($max_width) { if ($img_width > $max_width) { $img_height = intval($max_width / $img_width * $img_height); $img_width = $max_width; $resize = true; } } if ($max_height) { if ($img_height > $max_height) { $img_width = intval($max_height / $img_height * $img_width); $img_height = $max_height; $resize = true; } } $path_parts = pathinfo($fileName); if ($resize) { if ($i['mime'] == 'image/jpeg') { $thumb = imagecreatetruecolor($img_width, $img_height); $source = imagecreatefromjpeg($fileName); imagecopyresampled($thumb, $source, 0, 0, 0, 0, $img_width, $img_height, $old_img_width, $old_img_height); imagejpeg($thumb, $path_parts['dirname'] . '/' . $asz[0] . '_' . $path_parts['basename']); } elseif ($i['mime'] == 'image/gif') { $thumb = imagecreate($img_width, $img_height); $source = imagecreatefromgif($fileName); imagecopyresized($thumb, $source, 0, 0, 0, 0, $img_width, $img_height, $old_img_width, $old_img_height); imagegif($thumb, $path_parts['dirname'] . '/' . $asz[0] . '_' . $path_parts['basename']); } elseif ($i['mime'] == 'image/png') { $thumb = imagecreatetruecolor($img_width, $img_height); $source = imagecreatefrompng($fileName); imagecopyresampled($thumb, $source, 0, 0, 0, 0, $img_width, $img_height, $old_img_width, $old_img_height); imagepng($thumb, $path_parts['dirname'] . '/' . $asz[0] . '_' . $path_parts['basename']); } imagedestroy($thumb); imagedestroy($source); } else { @copy($fileName, $path_parts['dirname'] . '/' . $asz[0] . '_' . $path_parts['basename']); } } } } }
function ImageResize($srcFile, $toW, $toH, $toFile = "") { if ($toFile == "") { $toFile = $srcFile; } $info = ""; $data = GetImageSize($srcFile, $info); switch ($data[2]) { case 1: if (!function_exists("imagecreatefromgif")) { echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>"; exit; } $im = ImageCreateFromGIF($srcFile); break; case 2: if (!function_exists("imagecreatefromjpeg")) { echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>"; exit; } $im = ImageCreateFromJpeg($srcFile); break; case 3: $im = ImageCreateFromPNG($srcFile); break; } $srcW = ImageSX($im); $srcH = ImageSY($im); $toWH = $toW / $toH; $srcWH = $srcW / $srcH; if ($toWH <= $srcWH) { $ftoW = $toW; $ftoH = $ftoW * ($srcH / $srcW); } else { $ftoH = $toH; $ftoW = $ftoH * ($srcW / $srcH); } if ($srcW > $toW || $srcH > $toH) { if (function_exists("imagecreatetruecolor")) { @($ni = ImageCreateTrueColor($ftoW, $ftoH)); if ($ni) { ImageCopyResampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } else { $ni = ImageCreate($ftoW, $ftoH); ImageCopyResized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } } else { $ni = ImageCreate($ftoW, $ftoH); ImageCopyResized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } if (function_exists('imagejpeg')) { ImageJpeg($ni, $toFile); } else { ImagePNG($ni, $toFile); } ImageDestroy($ni); } ImageDestroy($im); }
/** * Check if the file is an image * @param string $file * @return bool */ public function isItImage($file) { if ($img = @GetImageSize($file)) { return TRUE; } else { return FALSE; } }
public function download($method = "curl") { $info = @GetImageSize($this->source); $mime = $info['mime']; // What sort of image? $type = substr(strrchr($mime, '/'), 1); switch ($type) { case 'jpeg': $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; // Best Quality: 100 $quality = isset($this->quality) ? $this->quality : 100; break; case 'png': $image_create_func = 'ImageCreateFromPNG'; $image_save_func = 'ImagePNG'; $new_image_ext = 'png'; // Compression Level: from 0 (no compression) to 9 $quality = isset($this->quality) ? $this->quality : 0; break; case 'bmp': $image_create_func = 'ImageCreateFromBMP'; $image_save_func = 'ImageBMP'; $new_image_ext = 'bmp'; break; case 'gif': $image_create_func = 'ImageCreateFromGIF'; $image_save_func = 'ImageGIF'; $new_image_ext = 'gif'; break; default: $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; } if (isset($this->extension)) { $ext = strrchr($this->source, "."); $strlen = strlen($ext); $new_name = $this->filename . '.' . $new_image_ext; $this->filename = $new_name; } else { $new_name = basename($this->source); } $save_to = $this->path . $new_name; if ($method == 'curl') { $save_image = $this->LoadImageCURL($save_to); } elseif ($method == 'gd') { $img = $image_create_func($this->source); if (isset($quality)) { $save_image = $image_save_func($img, $save_to, $quality); } else { $save_image = $image_save_func($img, $save_to); } } $this->save(); return $save_image; }
function ResizeImage($Filename, $Thumbnail, $Size) { $Path = pathinfo($Filename); $Extension = $Path['extension']; $ImageData = @GetImageSize($Filename); $Width = $ImageData[0]; $Height = $ImageData[1]; if ($Width >= $Height and $Width > $Size) { $NewWidth = $Size; $NewHeight = $Size / $Width * $Height; } elseif ($Height >= $Width and $Height > $Size) { $NewWidth = $Size / $Height * $Width; $NewHeight = $Size; } else { $NewWidth = $Width; $NewHeight = $Height; } $NewImage = @ImageCreateTrueColor($NewWidth, $NewHeight); if (preg_match('/^gif$/i', $Extension)) { $Image = @ImageCreateFromGif($Filename); } elseif (preg_match('/^png$/i', $Extension)) { $Image = @ImageCreateFromPng($Filename); } else { $Image = @ImageCreateFromJpeg($Filename); } if ($ImageData[2] == IMAGETYPE_GIF or $ImageData[2] == IMAGETYPE_PNG) { $TransIndex = imagecolortransparent($Image); // If we have a specific transparent color if ($TransIndex >= 0) { // Get the original image's transparent color's RGB values $TransColor = imagecolorsforindex($Image, $TransIndex); // Allocate the same color in the new image resource $TransIndex = imagecolorallocate($NewImage, $TransColor['red'], $TransColor['green'], $TransColor['blue']); // Completely fill the background of the new image with allocated color. imagefill($NewImage, 0, 0, $TransIndex); // Set the background color for new image to transparent imagecolortransparent($NewImage, $TransIndex); } elseif ($ImageData[2] == IMAGETYPE_PNG) { // Turn off transparency blending (temporarily) imagealphablending($NewImage, false); // Create a new transparent color for image $color = imagecolorallocatealpha($NewImage, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($NewImage, 0, 0, $color); // Restore transparency blending imagesavealpha($NewImage, true); } } @ImageCopyResampled($NewImage, $Image, 0, 0, 0, 0, $NewWidth, $NewHeight, $Width, $Height); if (preg_match('/^gif$/i', $Extension)) { @ImageGif($NewImage, $Thumbnail); } elseif (preg_match('/^png$/i', $Extension)) { @ImagePng($NewImage, $Thumbnail); } else { @ImageJpeg($NewImage, $Thumbnail); } @chmod($Thumbnail, 0644); }
function mime_get_type_GetImageSize($filename) { $type_mapping = array("1" => "image/gif", "2" => "image/jpeg", "3" => "image/png", "4" => "application/x-shockwave-flash", "6" => "image/bmp"); @($size = GetImageSize($filename)); if ($size[2] && $type_mapping[$size[2]]) { return $type_mapping[$size[2]]; } return false; }