Example #1
0
 /**
  * 裁剪函数,两个参数表示需要的长宽,可以为百分比,表示目标大小为现在的百分之多少
  * @param number $width
  * @param number $height
  * @return GdImage
  */
 public function crop($width, $height)
 {
     $sourceWidth = $this->image->getWidth();
     $sourceHeight = $this->image->getHeight();
     if ($width < 1) {
         $width = $sourceWidth * $width;
     }
     if ($height < 1) {
         $height = $sourceHeight * $height;
     }
     $sourceHWRatio = $sourceHeight / $sourceWidth;
     $targetHWRatio = $height / $width;
     if ($sourceHWRatio > $targetHWRatio) {
         $ratio = 1.0 * $width / $sourceWidth;
     } else {
         $ratio = 1.0 * $height / $sourceHeight;
     }
     $tmp_w = (int) ($width / $ratio);
     $tmp_h = (int) ($height / $ratio);
     $tmp_x = (int) ($sourceWidth - $tmp_w) / 2;
     $tmp_y = (int) ($sourceHeight - $tmp_h) / 2;
     $targetImage = $this->image->initNew($width, $height);
     imagecopyresampled($targetImage->getImage(), $this->image->getImage(), 0, 0, $tmp_x, $tmp_y, $width, $height, $tmp_w, $tmp_h);
     return $targetImage;
 }
Example #2
0
 /**
  * Applies validator
  *
  * @return  boolean     success
  */
 public function main()
 {
     include_once NX_PATH_LIB . 'gd/gdimage.php';
     $gd = new GdImage();
     $this->message = 'is an invalid image type. supported types are: .' . implode($gd->getSupportedTypes(), ', .');
     $data = Nexista_Path::get($this->params['var'], 'flow');
     if (!empty($data)) {
         $this->result = $gd->isSupported($data);
         return true;
     }
     $this->setEmpty();
     return true;
 }
 /**
  * Widgets run function
  * @param integer $id
  * @param string $attribute
  * @throws CHttpException
  */
 public function run()
 {
     if (Yii::app()->getRequest()->isPostRequest) {
         if (!file_exists($this->uploadPath)) {
             throw new CHttpException(Yii::t('zii', 'Invalid upload path'));
         }
         $uploader = new qqFileUploader($this->allowedExtensions, $this->sizeLimit);
         $result = $uploader->handleUpload($this->uploadPath, true, session_id());
         $gd = new GdImage();
         // step 1: make a copy of the original
         $filePath = $this->uploadPath . '/' . $result['filename'];
         $copyName = $gd->createName($result['filename'], '_FULLSIZE');
         $gd->copy($filePath, $this->uploadPath . '/' . $copyName);
         // step 2: Scale down or up this image so it fits in the browser nicely, lets say 500px is safe
         $oldSize = $gd->getProperties($filePath);
         if ($oldSize['w'] >= $this->resizeWidth) {
             $newSize = $gd->getAspectRatio($oldSize['w'], $oldSize['h'], $this->resizeWidth, 0);
             $gd->resize($filePath, $newSize['w'], $newSize['h']);
         }
         echo json_encode($result);
         Yii::app()->end();
     } else {
         throw new CHttpException(Yii::t('zii', 'Invalid request'));
     }
 }
 /**
  * Widgets run function
  * @throws CHttpException
  */
 public function run()
 {
     if (Yii::app()->getRequest()->isPostRequest) {
         if (!file_exists($this->uploadPath)) {
             throw new CHttpException(Yii::t('zii', 'Invalid upload path'));
         }
         $gd = new GdImage();
         foreach ($_POST['imgcrop'] as $k => $v) {
             // 1) delete resized, move to full size
             $filePath = $this->uploadPath . '/' . $v['filename'];
             $fullSizeFilePath = $this->uploadPath . '/' . $gd->createName($v['filename'], '_FULLSIZE');
             unlink($filePath);
             rename($fullSizeFilePath, $filePath);
             // 2) compute the new coordinates
             $scaledSize = $gd->getProperties($filePath);
             if ($scaledSize['w'] >= $this->resizeWidth) {
                 $percentChange = $scaledSize['w'] / $this->resizeWidth;
                 // we know we scaled by width of px in upload
             } else {
                 $percentChange = 1;
             }
             $newCoords = array('x' => $v['x'] * $percentChange, 'y' => $v['y'] * $percentChange, 'w' => $v['w'] * $percentChange, 'h' => $v['h'] * $percentChange);
             // 3) crop the full size image
             $gd->crop($filePath, $newCoords['x'], $newCoords['y'], $newCoords['w'], $newCoords['h']);
             // 4) resize the cropped image to whatever size we need (lets go with 200 wide)
             $ar = $gd->getAspectRatio($newCoords['w'], $newCoords['h'], $this->cropWidth, 0);
             $gd->resize($filePath, $ar['w'], $ar['h']);
         }
         echo '1';
         Yii::app()->end();
     } else {
         throw new CHttpException(Yii::t('zii', 'Invalid request'));
     }
 }
Example #5
0
<?php

// ideally one day this can do more than one image...
// they would be stacked up to crop all at once in
// Impromptu.. thus returning an array
date_default_timezone_set('UTC');
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
define('DS', DIRECTORY_SEPARATOR);
define('CURR_DIR', dirname(__FILE__) . DS);
define('UPLOAD_DIR', CURR_DIR . "uploads" . DS);
//crop our image..
require "gd_image.php";
$gd = new GdImage();
foreach ($_POST['imgcrop'] as $k => $v) {
    $targetPath = UPLOAD_DIR;
    $targetFile = str_replace('//', '/', $targetPath) . $v['filename'];
    $gd->crop($targetFile, $v['x'], $v['y'], $v['w'], $v['h']);
    //generate thumb or whatever else you like...
}
echo "1";
Example #6
0
 /**
  * Resize an GD image
  *
  * @return boolean False if the target image cannot be created, otherwise true
  */
 protected function executeResizeGd()
 {
     $sourceImage = \GdImage::fromFile($this->fileObj);
     $coordinates = $this->computeResize();
     $newImage = \GdImage::fromDimensions($coordinates['width'], $coordinates['height']);
     $sourceImage->copyTo($newImage, $coordinates['target_x'], $coordinates['target_y'], $coordinates['target_width'], $coordinates['target_height']);
     $newImage->saveToFile(TL_ROOT . '/' . $this->getCacheName());
 }
Example #7
0
 /**
  * 添加水印
  */
 public function album_pic_watermarkOp()
 {
     if (empty($_POST['id']) && !is_array($_POST['id'])) {
         showDialog(Language::get('album_parameter_error'));
     }
     $id = trim(implode(',', $_POST['id']), ',');
     /**
      * 实例化图片模型
      */
     $model_album = Model('album');
     $param['in_apic_id'] = $id;
     $param['store_id'] = $_SESSION['store_id'];
     $wm_list = $model_album->getPicList($param);
     $model_store_wm = Model('store_watermark');
     $store_wm_info = $model_store_wm->getOneStoreWMByStoreId($_SESSION['store_id']);
     if ($store_wm_info['wm_image_name'] == '' && $store_wm_info['wm_text'] == '') {
         showDialog(Language::get('album_class_setting_wm'), "index.php?act=store_album&op=store_watermark", 'error', 'CUR_DIALOG.close();');
         //"请先设置水印"
     }
     import('libraries.gdimage');
     $gd_image = new GdImage();
     $gd_image->setWatermark($store_wm_info);
     foreach ($wm_list as $v) {
         $gd_image->create(BASE_UPLOAD_PATH . DS . ATTACH_GOODS . DS . $_SESSION['store_id'] . DS . str_ireplace('.', '_1280.', $v['apic_cover']));
         //生成有水印的大图
     }
     showDialog(Language::get('album_pic_plus_wm_succeed'), 'reload', 'succ');
 }
Example #8
0
<?php

// ideally one day this can do more than one image...
// they would be stacked up to crop all at once in
// Impromptu.. thus returning an array
date_default_timezone_set('UTC');
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
define('DS', DIRECTORY_SEPARATOR);
define('CURR_DIR', dirname(__FILE__) . DS);
define('UPLOAD_DIR', CURR_DIR . "uploads" . DS);
require "gd_image.php";
$gd = new GdImage();
foreach ($_POST['imgcrop'] as $k => $v) {
    /*
    	1) delete the resized image from upload, we will only be working with the full size
    	2) compute new coordinates of full size image
    	3) crop full size image
    	4) resize the cropped image to what ever size we need
    */
    // 1) delete resized, move to full size
    $filePath = UPLOAD_DIR . $v['filename'];
    $fullSizeFilePath = UPLOAD_DIR . $gd->createName($v['filename'], '_FULLSIZE');
    unlink($filePath);
    rename($fullSizeFilePath, $filePath);
    // 2) compute the new coordinates
    $scaledSize = $gd->getProperties($filePath);
    $percentChange = $scaledSize['w'] / 500;
    // we know we scaled by width of 500 in upload
    $newCoords = array('x' => $v['x'] * $percentChange, 'y' => $v['y'] * $percentChange, 'w' => $v['w'] * $percentChange, 'h' => $v['h'] * $percentChange);
Example #9
0
 /**
  * 上传图片
  *
  * @param 
  * @return 
  */
 public function swfuploadOp()
 {
     /**
      * 读取语言包
      */
     Language::read('iswfupload');
     $lang = Language::getLangContent();
     // 图片上传session传递
     if (isset($_POST["PHPSESSID"])) {
         session_id($_POST["PHPSESSID"]);
     }
     /**
      * 上传图片
      */
     $upload = new UploadFile();
     $upload_dir = ATTACH_GOODS . DS . $_SESSION['store_id'] . DS;
     //		/**
     //		 * 上传图片前,对卖家的使用空间进行判断
     //		 */
     //		$model_store_grade	= Model('store_grade');
     //		$store_grade	= $model_store_grade->getGradeShopList(array('store_id'=>$_SESSION['store_id']));
     //		if(intval($store_grade[0]['sg_space_limit']) != 0) {
     //			$use_space = number_format((getDirSize($upload_dir)/1024/1024),2,'.','');
     //			$grade_space = number_format($store_grade[0]['sg_space_limit'],2,'.','');
     //			if($use_space >= $grade_space) {
     //				echo json_encode(array('state'=>'false','message'=>$lang['iswfupload_reach_limit'].$grade_space.$lang['iswfupload_upgrade']));
     //				exit();
     //			}
     //		}
     /**
      * 设置上传图片路径
      */
     $upload->set('default_dir', $upload_dir . $upload->getSysSetPath());
     if (trim($_GET['instance']) == 'goods_image') {
         $upload->set('ifresize', true);
     }
     $result = $upload->upfile('Filedata');
     if (empty($upload->error_msg)) {
         if (trim($_GET['instance']) == 'goods_image') {
             $_POST['pic'] = $upload->getSysSetPath() . $result['image'];
             $_POST['pic_thumb'] = $upload->getSysSetPath() . $result['image_thumb'];
             $model_store_wm = Model('store_watermark');
             /**
              * 获取会员水印设置
              */
             $store_wm_info = $model_store_wm->getOneStoreWMByStoreId($_SESSION['store_id']);
             /**
              * 是否开启是否开启水印
              */
             if ($store_wm_info['wm_is_open']) {
                 require_once BasePath . DS . 'framework' . DS . 'libraries' . DS . 'gdimage.php';
                 $gd_image = new GdImage();
                 $gd_image->setWatermark($store_wm_info);
                 $gd_image->create(ATTACH_GOODS . DS . $_SESSION['store_id'] . DS . $upload->getSysSetPath() . $result['image_thumb']);
                 //缩略图加水印
                 $gd_image->set('save_file', ATTACH_GOODS . DS . $_SESSION['store_id'] . DS . $upload->getSysSetPath() . 'wm_' . $result['image']);
                 $gd_image->set('src_image_name', ATTACH_GOODS . DS . $_SESSION['store_id'] . DS . $upload->getSysSetPath() . $result['image']);
                 $gd_create = $gd_image->create();
                 //生成有水印的大图
                 if ($gd_create) {
                     $_POST['pic_wm'] = $upload->getSysSetPath() . 'wm_' . $result['image'];
                 }
             }
             unset($store_wm_info);
         } else {
             $_POST['pic'] = $upload->getSysSetPath() . $result;
         }
     } else {
         echo json_encode(array('state' => 'false', 'message' => $lang['iswfupload_upload_pic_fail']));
         exit;
     }
     /**
      * 模型实例化
      */
     $model_upload = Model('upload');
     /**
      * 图片数据入库
      */
     $insert_array = array();
     $image_type = array('goods_image' => 2, 'desc_image' => 3);
     $insert_array['file_name'] = $_POST['pic'];
     $insert_array['file_thumb'] = empty($_POST['pic_thumb']) ? $_POST['pic'] : $_POST['pic_thumb'];
     $insert_array['file_wm'] = $_POST['pic_wm'];
     $insert_array['file_size'] = intval($_FILES['Filedata']['size']);
     $insert_array['upload_time'] = time();
     $insert_array['item_id'] = intval($_POST['item_id']);
     $insert_array['store_id'] = $_SESSION['store_id'];
     $insert_array['upload_type'] = $image_type[trim($_GET['instance'])];
     $result = $model_upload->add($insert_array);
     if ($result) {
         if ($_POST['pic_wm']) {
             $_POST['pic'] = $_POST['pic_wm'];
         }
         $data = array();
         $data['file_id'] = $result;
         $data['file_name'] = $_POST['pic'];
         $data['file_path'] = $_POST['pic'];
         $data['instance'] = $_GET['instance'];
         /**
          * 整理为json格式
          */
         $output = json_encode($data);
         echo $output;
     }
 }
Example #10
0
I have altered the fileuploader.php to pass back the filename 
which was set and the original filename
*/
require "../scripts" . DS . "fileuploader" . DS . "fileuploader.php";
$allowedExtensions = array('jpeg', 'jpg', 'gif', 'png');
$sizeLimit = 2 * 1024 * 1024;
// max file size in bytes
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload(UPLOAD_DIR, false, md5(uniqid()));
//handleUpload($uploadDirectory, $replaceOldFile=FALSE, $filename='')
/* 
For your crop you should:
1) Make a copy of the full size original
2) Scale down or up this image so it fits in the browser nicely
3) When the crop occurs we will manipulate the full size image
*/
require "gd_image.php";
$gd = new GdImage();
// step 1: make a copy of the original
$filePath = UPLOAD_DIR . $result['filename'];
$copyName = $gd->createName($result['filename'], '_FULLSIZE');
$gd->copy($filePath, UPLOAD_DIR . $copyName);
// step 2: Scale down or up this image so it fits in the browser nicely, lets say 500px is safe
$oldSize = $gd->getProperties($filePath);
$newSize = $gd->getAspectRatio($oldSize['w'], $oldSize['h'], 500, 0);
$gd->resize($filePath, $newSize['w'], $newSize['h']);
// step 3: handled in crop.php!
// to pass data through iframe you will need to encode all html tags
echo json_encode($result);
exit;
Example #11
0
 /**
  * 添加水印
  */
 public function album_pic_watermarkOp()
 {
     if (empty($_GET['id']) && !is_array($_GET['id'])) {
         showMessage(Language::get('album_parameter_error'), '', 'html', 'error');
     }
     $id = trim(implode(',', $_GET['id']), ',');
     /**
      * 实例化图片模型
      */
     $model_album = Model('album');
     $param['in_apic_id'] = $id;
     $param['store_id'] = $_SESSION['store_id'];
     $wm_list = $model_album->getPicList($param);
     $model_store_wm = Model('store_watermark');
     /**
      * 获取会员水印设置
      */
     $store_wm_info = $model_store_wm->getOneStoreWMByStoreId($_SESSION['store_id']);
     if ($store_wm_info['wm_image_name'] == '' && $store_wm_info['wm_text'] == '') {
         showMessage(Language::get('album_class_setting_wm'), "index.php?act=store_album&op=store_watermark", 'html', 'error');
         //"请先设置水印"
     }
     require_once BasePath . DS . 'framework' . DS . 'libraries' . DS . 'gdimage.php';
     $gd_image = new GdImage();
     $gd_image->setWatermark($store_wm_info);
     foreach ($wm_list as $v) {
         //缩略图不应该加水印
         //$gd_image->create(ATTACH_GOODS.DS.$_SESSION['store_id'].DS.$v['apic_cover'].'_small.'.get_image_type($v['apic_cover']));
         $gd_image->create(ATTACH_GOODS . DS . $_SESSION['store_id'] . DS . $v['apic_cover'] . '_max.' . get_image_type($v['apic_cover']));
         //生成有水印的大图
     }
     unset($store_wm_info);
     showMessage(Language::get('album_pic_plus_wm_succeed'));
 }