/** * Using GD lib for get and transform images. * @see http://php.net/manual/en/function.imagecreatefromjpeg.php#110547 */ function anyImgFile2Im($fname = NULL) { if ($fname) { $this->setFile($fname); } $allowedTypes = [1, 2, 3, 6]; // gif,jpg,png,bmp if (!in_array($this->ftype, $allowedTypes)) { return false; } switch ($this->ftype) { case 1: $im = imageCreateFromGif($this->fname); break; case 2: $im = imageCreateFromJpeg($this->fname); break; case 3: $im = imageCreateFromPng($this->fname); // echo "\n degug {$this->fname}"; break; case 6: $im = imageCreateFromBmp($this->fname); break; } return $im; }
function img_create($type, $name) { if ($type == "gif") { $im = imageCreateFromGif($name); } elseif ($type == "jpeg" || $type == "jpg") { $im = imagecreatefromjpeg($name); } elseif ($type == "png") { $im = imageCreateFromPng($name); } elseif ($type == "bmp") { $im = imageCreateFromBmp($name); } else { return false; } return $im; }
/** Cắt ảnh theo cỡ tùy chọn */ function create_image($args) { $hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET); hook_filter('before_create_image', $args); hook_action('create_image'); if (!is_array($args)) { parse_str($args, $args); } $file_id = $args['file']; if (isset_image($file_id)) { $row = get_file_data($file_id); $file_info = json_decode($row->file_info); $file_dst_name_body = $file_info->file_dst_name_body; $file_dst_name_ext = $file_info->file_dst_name_ext; $source_file = get_file_location($file_id); $crop_name = $file_dst_name_body . '-' . $args['w'] . 'x' . $args['h'] . '.' . $file_dst_name_ext; if (file_exists(get_file_location($file_id, FALSE) . $crop_name)) { return get_file_url($file_id, FALSE) . $crop_name; } else { /** resize file */ /* fix func exif_imagetype not avaiable */ $type = getimagesize($source_file); $type = $type['mime']; switch ($type) { case 'image/png': $type = IMAGETYPE_PNG; break; case 'image/jpeg': $type = IMAGETYPE_JPEG; break; case 'image/gif': $type = IMAGETYPE_GIF; break; case 'image/bmp': $type = IMAGETYPE_BMP; break; case 'image/x-ms-bmp': $type = IMAGETYPE_BMP; break; } /* fix func exif_imagetype not avaiable */ switch ($type) { case 1: $source = imageCreateFromGif($source_file); break; case 2: $source = imageCreateFromJpeg($source_file); break; case 3: $source = imageCreateFromPng($source_file); break; case 6: $source = imageCreateFromBmp($source_file); break; } /** resize file gốc về cùng 1 cỡ */ $size = getimagesize($source_file); $source_width = $size[0]; $source_height = $size[1]; $fix_width = $args['w']; $fix_height = $args['h']; $thumb = imagecreatetruecolor($fix_width, $fix_height); /* Fix black background */ $white = imagecolorallocate($thumb, 255, 255, 255); imagefill($thumb, 0, 0, $white); /* Fix black background */ /* fix quality with imagecopyresampled , repalce imagecopyresized */ imagecopyresampled($thumb, $source, 0, 0, 0, 0, $fix_width, $fix_height, $source_width, $source_height); $saveto = get_file_location($file_id, FALSE) . $crop_name; imagejpeg($thumb, $saveto, 100); return get_file_url($file_id, FALSE) . $crop_name; } } else { return FALSE; } }
/** * Original source: https://stackoverflow.com/questions/12661/efficient-jpeg-image-resizing-in-php * Resize images for thumbnails/mobile * @param str $sourcefile * @param str $endfile * @param int $thumbwidth * @param int $thumbheight * @param int $quality */ public function makeThumbnail($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality) { // Takes the sourcefile (path/to/image.jpg) and makes a thumbnail from it // and places it at endfile (path/to/thumb.jpg). // Load image and get image size. $type = exif_imagetype($sourcefile); // [] if you don't have exif you could use getImageSize() switch ($type) { case 1: $img = imageCreateFromGif($sourcefile); break; case 2: $img = imageCreateFromJpeg($sourcefile); break; case 3: $img = imageCreateFromPng($sourcefile); break; case 6: $img = imageCreateFromBmp($sourcefile); break; } $width = imagesx($img); $height = imagesy($img); // Don't make images larger than the original if ($thumbwidth > $width) { $thumbwidth = $width; } if ($thumbheight > $height) { $thumbheight = $height; } // Resized dimensions $newwidth = $thumbwidth; $divisor = $width / $thumbwidth; $newheight = floor($height / $divisor); // Create a new temporary image. $tmpimg = imagecreatetruecolor($newwidth, $newheight); // Copy and resize old image into new image. imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Save thumbnail into a file. $returnVal = imagejpeg($tmpimg, $endfile, $quality); // Release the memory imagedestroy($tmpimg); imagedestroy($img); return $returnVal; }
/** * @author Matt Squirrell * @source http://php.net/manual/fr/function.imagecreatefromjpeg.php * @licence none * * @param <i>String</i> url de l'image * @return <i>Image</i> copie de l'image */ protected function imageCreateFromAny($filepath) { $type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize() $allowedTypes = array(1, 2, 3, 6); if (!in_array($type, $allowedTypes)) { return "error"; } switch ($type) { case 1: $im = imageCreateFromGif($filepath); break; case 2: $im = imageCreateFromJpeg($filepath); break; case 3: $im = imageCreateFromPng($filepath); break; case 6: $im = imageCreateFromBmp($filepath); break; } return $im; }
/** * [resizeImage description] * @param [type] $im [源目标图片] * @param [type] $maxwidth [最大宽度] * @param [type] $maxheight [最大高度] * @param [type] $name [图片名] * @param [type] $filetype [图片类型] * @param [type] $tmp_name [上传的文件的临时路径] * @return [type] [成功true] */ function resizeImage($tmp_name, $maxwidth, $maxheight, $name, $filetype) { try { $img_info = getimagesize($tmp_name); if (!in_array($img_info['mime'], array('image/jpeg', 'image/png', 'image/bmp', 'image/gif', 'image/pjpeg', 'image/jpg', 'image/x-png'))) { $this->errmsg = '只支持上传图片'; return FALSE; } $pic_width = $img_info[0]; $pic_height = $img_info[1]; if ($maxwidth && $pic_width > $maxwidth || $maxheight && $pic_height > $maxheight) { $resizeheightTag = $resizewidthTag = false; if ($maxwidth && $pic_width > $maxwidth) { $widthratio = $maxwidth / $pic_width; $resizewidthTag = true; } if ($maxheight && $pic_height > $maxheight) { $heightratio = $maxheight / $pic_height; $resizeheightTag = true; } if ($resizewidthTag && $resizeheightTag) { if ($widthratio < $heightratio) { $ratio = $widthratio; } else { $ratio = $heightratio; } } if ($resizewidthTag && !$resizeheightTag) { $ratio = $widthratio; } if ($resizeheightTag && !$resizewidthTag) { $ratio = $heightratio; } $newwidth = $pic_width * $ratio; $newheight = $pic_height * $ratio; $newim = imagecreatetruecolor($newwidth, $newheight); switch ($img_info['mime']) { case "image/gif": $images = imagecreatefromgif($tmp_name); break; case "image/pjpeg": case "image/jpeg": case "image/jpg": $images = imagecreatefromjpeg($tmp_name); break; case "image/png": case "image/x-png": $images = imagecreatefrompng($tmp_name); break; case "image/bmp": $images = imageCreateFromBmp($tmp_name); break; } imagecopyresampled($newim, $images, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); $name = $this->save_path . $name . $filetype; switch ($img_info['mime']) { case "image/gif": imagegif($newim, $name); break; case "image/pjpeg": case "image/jpeg": case "image/jpg": imagejpeg($newim, $name, 100); break; case "image/png": case "image/x-png": imagepng($newim, $name); break; case "image/bmp": imagebmp($newim, $name); break; } imagedestroy($newim); } else { if (!copy($tmp_name, $this->save_path . $name . $filetype)) { return false; } } return TRUE; } catch (E $e) { return FALSE; } }
$allowedTypes = array(1, 2, 3, 6); if (!in_array($type, $allowedTypes)) { return false; } switch ($type) { case 1: $im = imageCreateFromGif($filepath); break; case 2: $im = imageCreateFromJpeg($filepath); break; case 3: $im = imageCreateFromPng($filepath); break; case 6: $im = imageCreateFromBmp($filepath); break; } //print_r($data); // Rotate $degrees = 90; $rotate = imagerotate($im, $degrees, 0); switch ($type) { case 1: imagegif($rotate, $filepath, 100); break; case 2: imagejpeg($rotate, $filepath, 100); break; case 3: imagepng($rotate, $filepath, 100);
/** * Create a thumbnail image from $inputFileName no taller or wider than * $maxSize. Returns the new image resource or false on error. * Author: mthorn.net */ public function thumbnail($inputFileName, $maxSize = 75) { $info = getimagesize($inputFileName); $type = isset($info['type']) ? $info['type'] : $info[2]; // Check support of file type if (!(imagetypes() & $type)) { // Server does not support file type return false; } $width = isset($info['width']) ? $info['width'] : $info[0]; $height = isset($info['height']) ? $info['height'] : $info[1]; // Calculate aspect ratio $wRatio = $maxSize / $width; $hRatio = $maxSize / $height; // ini_set("gd.jpeg_ignore_warning", 1); switch ($type) { case 1: $sourceImage = imageCreateFromGif($inputFileName); break; case 2: $sourceImage = imageCreateFromJpeg($inputFileName); break; case 3: $sourceImage = imageCreateFromPng($inputFileName); break; case 6: $sourceImage = imageCreateFromBmp($inputFileName); break; } // Calculate a proportional width and height no larger than the max size. if ($width <= $maxSize && $height <= $maxSize) { // Input is smaller than thumbnail, do nothing return $sourceImage; } elseif ($wRatio * $height < $maxSize) { // Image is horizontal $tHeight = ceil($wRatio * $height); $tWidth = $maxSize; } else { // Image is vertical $tWidth = ceil($hRatio * $width); $tHeight = $maxSize; } $thumb = imagecreatetruecolor($tWidth, $tHeight); if ($sourceImage === false) { // Could not load image return false; } // Copy resampled makes a smooth thumbnail imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height); imagedestroy($sourceImage); return $thumb; }
/** * [resizeImage description] * @param [type] $im [源目标图片] * @param [type] $maxwidth [最大宽度] * @param [type] $maxheight [最大高度] * @param [type] $name [图片名] * @param [type] $filetype [图片类型] * @param [type] $tmp_name [上传的文件的临时路径] * @return [type] [成功true] */ function resizeImage($tmp_name, $maxwidth, $maxheight, $name, $filetype) { try { $img_info = getimagesize($tmp_name); if (!in_array($img_info['mime'], array('image/jpeg', 'image/png', 'image/bmp', 'image/gif'))) { $this->errmsg = self::FAILED_IMG_TYPE; return FALSE; } //判断是否需要创建文件夹 if (!$this->_createdir($this->save_path)) { return self::FAILED_CREATE_DIR; } $pic_width = $img_info[0]; $pic_height = $img_info[1]; if ($maxwidth && $pic_width > $maxwidth || $maxheight && $pic_height > $maxheight) { if ($maxwidth && $pic_width > $maxwidth) { $widthratio = $maxwidth / $pic_width; $resizewidth_tag = true; } else { $resizewidth_tag = false; } if ($maxheight && $pic_height > $maxheight) { $heightratio = $maxheight / $pic_height; $resizeheight_tag = true; } else { $resizeheight_tag = false; } if ($resizewidth_tag && $resizeheight_tag) { if ($widthratio < $heightratio) { $ratio = $widthratio; } else { $ratio = $heightratio; } } if ($resizewidth_tag && !$resizeheight_tag) { $ratio = $widthratio; } if ($resizeheight_tag && !$resizewidth_tag) { $ratio = $heightratio; } $newwidth = $pic_width * $ratio; $newheight = $pic_height * $ratio; $newim = imagecreatetruecolor($newwidth, $newheight); switch ($img_info[2]) { case 1: $images = imageCreateFromGif($tmp_name); break; case 2: $images = imageCreateFromJpeg($tmp_name); break; case 3: $images = imageCreateFromPng($tmp_name); break; case 6: $images = imageCreateFromBmp($tmp_name); break; } imagecopyresampled($newim, $images, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); $name = $this->save_path . $name . $filetype; imagejpeg($newim, $name); imagedestroy($newim); } else { $name = $this->save_path . $name . $filetype; if (!move_uploaded_file($tmp_name, $name)) { return false; } } return TRUE; } catch (E $e) { return FALSE; } }