webyjh 2011-12-31 10:13:33 5062次浏览 4条回复 0 0 0

缩略图扩展cthumb好像有问题: 当原图大于缩略图的设置时,正常的 当原图小于缩略图的设置时,缩略图的文件名会多加一个扩展名,造成图片不显示,

public function actionCreate()
{
    $model=new News;

    // Uncomment the following line if AJAX validation is needed
    $this->performAjaxValidation($model);

    if(isset($_POST['News']))
    {
        $model->attributes=$_POST['News'];
        $model->front_img=CUploadedFile::getInstance($model,'front_img');
        if($model->save())
        {                 
            //按时间生成新文件名
            $newName=date('YmdHis').'_'.rand(1,9).'.'.$model->front_img->extensionName;
            //设定上传目录
            $path=yii::app()->basePath.'/../uploads/images/'.$newName;
            //上传图片
            $model->front_img->saveAs($path);                
            //利用扩展生成缩略图
            $im = null;
            //取图片类型
            $imagetype = strtolower($model->front_img->extensionName);
            if($imagetype == 'gif')
                $im = imagecreatefromgif($path);
            else if ($imagetype == 'jpg')
                $im = imagecreatefromjpeg($path);
            else if ($imagetype == 'png')
                $im = imagecreatefrompng($path);
            //设定缩略图的存放目录+名字
            $thumb_path=yii::app()->basePath.'/../uploads/thumb_images/'.$newName;
            //生成缩略图
            CThumb::resizeImage($im,410,250,$thumb_path,$model->front_img->extensionName);
                
            // redirect to success page
            $this->redirect(array('view','id'=>$model->id));                
            }        
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

是不是要在生成前判断一下原图的大小

  • 回复于 2011-12-31 10:37 举报

    原来扩展里有这一句,应该是多余了

    //$name = $name . $filetype;//这句好像多余了

    <?php
    class CThumb extends CApplicationComponent {
        public static function resizeImage($im, $maxwidth, $maxheight, $name,$filetype) {
            $pic_width = imagesx ( $im );
            $pic_height = imagesy ( $im );
    
            if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
                if ($maxwidth && $pic_width > $maxwidth) {
                    $widthratio = $maxwidth / $pic_width;
                    $resizewidth_tag = true;
                }
                
                if ($maxheight && $pic_height > $maxheight) {
                    $heightratio = $maxheight / $pic_height;
                    $resizeheight_tag = true;
                }
                
                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;
                
                if (function_exists ( "imagecopyresampled" )) {
                    $newim = imagecreatetruecolor ( $newwidth, $newheight );
                    imagecopyresampled ( $newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height );
                } else {
                    $newim = imagecreate ( $newwidth, $newheight );
                    imagecopyresized ( $newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height );
                }
                
                imagejpeg ( $newim, $name );
                imagedestroy ( $newim );
            } else {
                //$name = $name . $filetype;//这句好像多余了
                imagejpeg ( $im, $name );
            }
        }
    }
    ?>
    
  • 回复于 2012-08-24 10:10 举报

    扩展在哪里下载?

  • 回复于 2012-08-24 16:05 举报

    想问一下,在视图里得怎么调用才能显示缩略图?

  • 回复于 2017-02-08 10:16 举报

    我想问下 我按照你的这个步骤都弄好了 但是缩略图跟原图一样没变化啊 怎么回事啊??

您需要登录后才可以回复。登录 | 立即注册