Ejemplo n.º 1
0
/**
 * Author : smallchicken
 * Time   : 2009年6月8日16:46:05
 * Last Time: 2010年5月5日 10:24:30
 * mode 1 : 强制裁剪,生成图片严格按照需要,不足放大,超过裁剪,图片始终铺满
 * mode 2 : 和1类似,但不足的时候 不放大 会产生补白,可以用png消除。
 * mode 3 : 只缩放,不裁剪,保留全部图片信息,会产生补白,
 * mode 4 : 只缩放,不裁剪,保留全部图片信息,此时的参数只是限制了生成的图片的最大宽高,不产生补白
 * mode 5 : 生成的图比例严格按照需要的比例,宽和高不超过给定的参数。
 * 默认补白为白色,如果要使补白成透明像素,请使用SaveAlpha()方法代替SaveImage()方法
 *
 * 调用方法:
 *
 * $ic=new ImageCrop('old.jpg','afterCrop.jpg');
 * $ic->Crop(120,80,2);
 * $ic->SaveImage();
 *        //$ic->SaveAlpha();将补白变成透明像素保存
 * $ic->destory();
 *
 *
 */
function make_thumb($src, $dst, $width, $height, $mode)
{
    $ic = new ImageCrop($src, $dst);
    $ic->Crop($width, $height, $mode);
    $ic->SaveImage();
    $ic->destory();
}
Ejemplo n.º 2
0
 public static function processAvatar($model, $source, $type = "artist")
 {
     try {
         $fileSystem = new Filesystem();
         $alowSize = Yii::app()->params['imageSize'];
         $maxSize = max($alowSize);
         $folderMax = "s0";
         foreach ($alowSize as $folder => $size) {
             // Create folder by ID
             $fileSystem->mkdirs($model->getAvatarPath($model->id, $folder, true));
             @chmod($model->getAvatarPath($model->id, $folder, true), 0755);
             // Get link file by ID
             $savePath[$folder] = $model->getAvatarPath($model->id, $folder);
             if ($size == $maxSize) {
                 $folderMax = $folder;
             }
         }
         // Delete file if exists
         if (file_exists($savePath[$folder])) {
             $fileSystem->remove($savePath);
         }
         if (file_exists($source)) {
             list($width, $height) = getimagesize($source);
             $imgCrop = new ImageCrop($source, 0, 0, $width, $height);
             // aspect ratio for image size
             $aspectRatioW = $aspectRatioH = 1;
             if ($type == "video") {
                 $videoAspectRatio = Yii::app()->params['videoResolutionRate'];
                 list($aspectRatioW, $aspectRatioH) = explode(":", $videoAspectRatio);
             }
             $res = array();
             foreach ($savePath as $k => $v) {
                 $desWidth = $alowSize[$k];
                 $desHeight = round($alowSize[$k] * intval($aspectRatioH) / intval($aspectRatioW));
                 if (file_exists($v) && is_file($v)) {
                     @unlink($v);
                 }
                 if ($width > 4000) {
                     self::ImageCropPro($v, $source, $desWidth, $desHeight, 70);
                 } else {
                     if ($k == $folderMax) {
                         $imgCrop->resizeRatio($v, $desWidth, $desHeight, 70);
                     } else {
                         $imgCrop->resizeCrop($v, $desWidth, $desHeight, 70);
                     }
                 }
             }
             if ($type != "video") {
                 $fileSystem->remove($source);
             }
         }
     } catch (Exception $e) {
         $error = $e->getMessage();
     }
 }
Ejemplo n.º 3
0
 /**
  * 生成并输出图片
  * 
  * @access public
  * @param mixed $src
  * @param mixed $dst
  * @param mixed $width
  * @param mixed $height
  * @param mixed $mode
  * @return void
  */
 public function make_crop_thumb($src, $dst, $width, $height, $mode)
 {
     $ic = new ImageCrop($src, $dst);
     $ic->Crop($width, $height, $mode);
     list($width, $height, $type) = getimagesize($src);
     if ($type === IMAGETYPE_PNG) {
         $ic->SaveAlpha();
     } else {
         $ic->SaveImage();
     }
     $ic->destory();
 }
Ejemplo n.º 4
0
 public static function processAvatar($id, $source, $type = "blog")
 {
     $fileSystem = new Filesystem();
     $alowSize = Yii::app()->params['imageSize']["{$type}"];
     $maxSize = max($alowSize);
     $folderMax = "s0";
     $pathDir = Yii::app()->params[$type . '_path'];
     foreach ($alowSize as $folder => $size) {
         // Create folder by ID
         $avatarPath = self::getAvatarPath($id, $folder, true, $pathDir);
         $fileSystem->mkdirs($avatarPath);
         @chmod($avatarPath, 0777);
         // Get link file by ID
         $savePath[$folder] = self::getAvatarPath($id, $folder, false, $pathDir);
         if ($size == $maxSize) {
             $folderMax = $folder;
         }
     }
     // Delete file if exists
     if (file_exists($savePath[$folder])) {
         $fileSystem->remove($savePath);
     }
     if (file_exists($source)) {
         list($width, $height) = getimagesize($source);
         $imgCrop = new ImageCrop($source, 0, 0, $width, $height);
         // aspect ratio for image size
         $aspectRatioW = $aspectRatioH = 1;
         foreach ($savePath as $k => $v) {
             $desWidth = $alowSize[$k];
             $desHeight = round($alowSize[$k] * intval($aspectRatioH) / intval($aspectRatioW));
             if (file_exists($v) && is_file($v)) {
                 @unlink($v);
             }
             if ($k == $folderMax) {
                 $imgCrop->resizeRatio($v, $desWidth, $desHeight, 100);
             } else {
                 $imgCrop->resizeCrop($v, $desWidth, $desHeight, 100);
             }
         }
         //remove file source
         $fileSystem->remove($source);
     }
 }
 protected function runInternal()
 {
     list($originalWidth, $originalHeight) = getimagesize($this->filePath);
     // Defaults offset - center
     $minOriginalSize = min($originalWidth, $originalHeight);
     if ($this->width > $this->height) {
         $cropWidth = $minOriginalSize;
         $cropHeight = (int) floor($minOriginalSize * ($this->height / $this->width));
     } else {
         $cropWidth = $minOriginalSize;
         $cropHeight = (int) floor($minOriginalSize * ($this->width / $this->height));
     }
     // Crop
     $cropProcessor = new ImageCrop(['filePath' => $this->filePath, 'width' => $cropWidth, 'height' => $cropHeight, 'thumbQuality' => $this->thumbQuality, 'offsetX' => round(($originalWidth - $cropWidth) / 2), 'offsetY' => round(($originalHeight - $cropHeight) / 2)]);
     $cropProcessor->run();
     // Resize
     $fitProcessor = new ImageResize(['filePath' => $this->filePath, 'width' => $this->width, 'height' => $this->height, 'thumbQuality' => $this->thumbQuality]);
     $fitProcessor->run();
     $this->width = $fitProcessor->width;
     $this->height = $fitProcessor->height;
 }
Ejemplo n.º 6
0
 public function resizeImage($source, $width, $height, $type = 'ratio')
 {
     $assetPath = $this->imgResizePath;
     $fullPathSource = $source;
     if (!file_exists($fullPathSource) || !is_file($fullPathSource)) {
         return false;
     }
     $ext = strtolower(substr(strrchr($source, '.'), 1));
     $fileName = strtolower(substr(strrchr($source, '/'), 1));
     $folderType = substr($type, 0, 1);
     $source = str_replace($this->imgOrgPath, "", $source);
     //$rzname = strtolower(substr($source, 0, strpos($source,'.')))."_{$width}_{$height}.{$ext}";
     $rzname = strtolower(substr($source, 0, strrpos($source, '/'))) . "/{$width}_{$height}_{$folderType}/{$fileName}";
     $rzname = strtolower(substr($rzname, 0, strpos($rzname, '.'))) . "/" . md5_file($fullPathSource) . ".jpg";
     if (file_exists($assetPath . DS . $rzname)) {
         return $rzname;
     }
     //empty dir first
     Utils::emptyDir(dirname($assetPath . DS . $rzname));
     Utils::makeDir($assetPath);
     if (!file_exists($assetPath) && !mkdir($assetPath, 0755)) {
         return $source;
     }
     //$folders = explode('/',$source);
     $folders = explode('/', $rzname);
     $tmppath = $assetPath . DS;
     for ($i = 0; $i < count($folders) - 1; $i++) {
         if (!file_exists($tmppath . $folders[$i]) && !mkdir($tmppath . $folders[$i], 0755)) {
             return $source;
         }
         $tmppath = $tmppath . $folders[$i] . '/';
     }
     if (!file_exists($assetPath . "/tmp")) {
         mkdir($assetPath . "/tmp", 0755);
     }
     list($r_width, $r_height) = getimagesize($fullPathSource);
     $imgCrop = new ImageCrop($fullPathSource, 0, 0, $r_width, $r_height, $assetPath . "/tmp");
     switch ($type) {
         case "fix":
             $imgCrop->resizeFix($assetPath . DS . $rzname, $width, $height, 90);
             break;
         case "ratio":
             $imgCrop->resizeRatio($assetPath . DS . $rzname, $width, $height, 90);
             break;
         case "crop":
         default:
             $imgCrop->resizeCrop($assetPath . DS . $rzname, $width, $height, 90);
             break;
     }
     @chmod($assetPath . DS . $rzname, 0775);
     return $rzname;
 }
Ejemplo n.º 7
0
    echo $_POST['top'] . $_POST['add'] . $_POST['pic_height'];
    $pic_height = $_POST['pic_height'];
    $pic_width = $_post['pic_width'];
    $top = $_POST['top'];
    $left = $_POST['left'];
    $target = $_POST['add'];
    $tempaddress = explode("/", $target);
    $tempaddress1 = end($tempaddress);
    $u_ID_avatar1 = explode('_', $tempaddress1);
    $u_ID_avatar = $u_ID_avatar1[0];
    $tumbname = $tempaddress1;
    $picname = str_replace('tumb', '', $tumbname);
    $targettumbdir = str_replace($tempaddress1, '', $target);
    $targettumb = $targettumbdir . $tumbname;
    $filename = $targettumbdir . $picname;
    $imageCrop = new ImageCrop();
    if ($imageCrop->openImage($filename)) {
        $imageCrop->crop($left, $top);
        $imageCrop->save($targettumb, $targettumbdir, $tumbname, $u_ID_avatar);
    }
}
class ImageCrop
{
    private $srcImage, $dstImage;
    private $width, $height;
    public function openImage($filename)
    {
        if (!file_exists($filename)) {
            return false;
        }
        $original = getimagesize($filename);
        //给画布分配颜色
        imagefill($this->dImage, 0, 0, $bg);
        //给图像用颜色进行填充
        imagecolortransparent($this->dImage, $bg);
        //背景定义成透明色
        $ratio_w = 1.0 * $dst_width / $this->src_width;
        //横向缩放的比例
        $ratio_h = 1.0 * $dst_height / $this->src_height;
        //纵向缩放的比例
        //var_dump($this);
        //不进行缩放,直接对图像进行裁剪
        $ratio = 1.0;
        $tmp_w = (int) ($dst_width / $ratio);
        $tmp_h = (int) ($dst_height / $ratio);
        $tmp_img = imagecreatetruecolor($dst_width, $dst_height);
        //创建暂时保存的画布
        imagecopy($tmp_img, $this->sImage, 0, 0, $dst_x, $dst_y, $dst_width, $dst_height);
        //拷贝出图像的一部分,进行裁切
        imagecopyresampled($this->dImage, $tmp_img, 0, 0, 0, 0, $dst_width, $dst_height, $tmp_w, $tmp_h);
        //把暂时缓存的图片,放到目标文件里面
        imagedestroy($tmp_img);
    }
}
$pathToFile = "E:/www2/GitHub/php_lib_code_center/qq_touxiang.jpg";
$targetFile = "E:/www2/GitHub/php_lib_code_center/" . time() . ".jpg";
$ic = new ImageCrop($pathToFile, $targetFile);
//$ic->Cut(40,30,120,130);
$ic->Crop(100, 100, 3, $targetFile);
$ic->SaveImage();
//$ic->SaveAlpha();将补白变成透明像素保存
$ic->destory();
Ejemplo n.º 9
0
 /**
  * upload file
  */
 protected function uploadFile($file, $model)
 {
     $coverPath = "";
     $fileSystem = new Filesystem();
     if (isset($file['error']) && $file['error'] == 0) {
         $ext = explode('.', $file['name']);
         $extFile = $ext[count($ext) - 1];
         $id = $model->id;
         $srcFileName = $id . time() . "." . $extFile;
         $tmpPath = Yii::app()->getRuntimePath();
         $fileDesPath = $tmpPath . DIRECTORY_SEPARATOR . $srcFileName;
         try {
             if (move_uploaded_file($file['tmp_name'], $fileDesPath)) {
                 list($width, $height) = getimagesize($fileDesPath);
                 if ($width < $this->coverWidth || $height < $this->coverHeight) {
                     return false;
                 }
                 $imgCrop = new ImageCrop($fileDesPath, 0, 0, $width, $height);
                 $coverPath = $model->getCoverPath($model->id);
                 Utils::makeDir(dirname($coverPath));
                 $imgCrop->resizeCrop($coverPath, $this->coverWidth, $this->coverHeight, 100);
                 $url = $model->getCoverUrl($model->id);
                 unlink($fileDesPath);
             }
         } catch (Exception $e) {
             echo $e->getMessage();
         }
     }
     return $url;
 }
Ejemplo n.º 10
0
 private function uploadImage($prefix_stored_filename, $input_filename, $path)
 {
     $save_name = $prefix_stored_filename . "_" . substr(md5("0_" . date("Y-m-d H:i:s") . $input_filename), 0, 12);
     $thumbnail_save_name = "thumb_" . $save_name;
     $fu = new FileUpload($_FILES[$input_filename]);
     $fu->setSave_name($save_name);
     $fu->setSave_path($path);
     if ($fu->doUpload()) {
         $im_crop = new ImageCrop($path . $fu->getSave_name());
         $im_crop->setSave_name($path . $thumbnail_save_name . "." . $fu->getExt());
         //$im_crop->setSave_path ( CATEGORY_IMAGE_PATH );
         list($width, $height) = $im_crop->getImage_size();
         //Scale the image if it is greater than the width set above
         if ($width > IC_CONF_IMAGE_THUMB_WIDTH) {
             $im_crop->setSave_scale(IC_CONF_IMAGE_THUMB_WIDTH / $width);
         } else {
             $im_crop->setSave_scale(1);
         }
         $im_crop->setSave_width($width);
         $im_crop->setSave_height($height);
         if (!$im_crop->doResize()) {
             print_r($im_crop->getErrorMessage());
             exit;
         }
         return $fu->getSave_name();
     } else {
         return false;
     }
 }
Ejemplo n.º 11
0
         if ($stmt_avatar) {
             $stmt_avatar->execute();
             $stmt_avatar->store_result();
             $stmt_avatar->bind_result($array_in_db);
             $stmt_avatar->fetch();
             $restore = unserialize($array_in_db);
             if ($restore) {
                 $push_newaddress = array_push($restore, $newname);
             } else {
                 $restore[0] = $newname;
             }
             $back = serialize($restore);
             $stmt_avatar_update = $mysqli->prepare("UPDATE `fireconverse`.`meminfo` SET `avatars`='{$back}' WHERE `ID`={$u_ID_avatar}");
             if ($stmt_avatar_update) {
                 $stmt_avatar_update->execute();
                 $imageCrop = new ImageCrop();
                 if ($imageCrop->openImage($target)) {
                     $imageCrop->crop();
                     $imageCrop->save($targettumb, $targettumbdir, $tumbname, $u_ID_avatar);
                 }
                 //else echo $newname;
                 //	echo "Upload Success!!";
             } else {
                 //echo 77;
             }
             $mysqli->close();
         } else {
             //echo 71;
         }
     }
 }
Ejemplo n.º 12
0
         $ft->define(array("main" => "template_index.html", "content" => "error.html"));
         $ft->assign("ERROR_MESSAGE", "ERROR OCURED");
     }
 } else {
     if ($all_url_vars["action"] == "save" && isset($all_url_vars["upload_thumbnail"])) {
         //	print CONF_INDEX_URL."tmp/".$_SESSION["uploaded_file"];
         //Get the new coordinates to crop the image.
         $x1 = $_POST["x1"];
         $y1 = $_POST["y1"];
         $x2 = $_POST["x2"];
         $y2 = $_POST["y2"];
         $w = $_POST["w"];
         $h = $_POST["h"];
         //	print $y1;
         //	error_log(print_r($_POST,true));
         $im_crop = new ImageCrop($_SESSION["path"] . $_SESSION["uploaded_file"]);
         //Scale the image to the thumb_width set above
         $im_crop->setSave_scale($thumb_width / $w);
         $im_crop->setSave_name($thumb_name);
         $im_crop->setSave_width($w);
         $im_crop->setSave_height($h);
         $im_crop->setStart_width($x1);
         $im_crop->setStart_height($y1);
         if (!$im_crop->doResize()) {
             print_r($im_crop->getErrorMessage());
             exit;
         }
         $im_crop->setSave_scale(1);
         $im_crop->setSave_name($_SESSION["path"] . $_SESSION["uploaded_file"]);
         if (!$im_crop->doResize()) {
             print_r($im_crop->getErrorMessage());