dqwh1203 2016-11-20 21:44
浏览 23
已采纳

无法通过php调整图像大小

i`m noobie in php, just creating image resize script. Following code is about what i try to do. Particularly - try to understand how to resize an image after addition , but face with some issue. Please, help me , what is wrong with my code ?

        $temp = getimagesize($_FILES['file']['tmp_name']); 
        $name = '/upload/'.date('Ymd-His').'img'. rand(10000, 99999).'.jpg';

        if (!in_array($matches[1], $array2)) {
            $errors['file'] = 'Wrong file extension';
        } elseif (!in_array($temp['mime'], $array)) { 
            $errors['file'] = 'Wrong type of file';
        } elseif (!move_uploaded_file($_FILES['file']['tmp_name'],'.'.$name)){ 
            $errors['file'] = 'image is not loaded';
        } else {

           // there is a problem

            $tmp = imagecreatetruecolor(200, 150);    
            $image = imagecreatefromjpeg($name);      
            imagecopyresampled($tmp, $image, 0,0,0,0, 200, 150, $temp[0], $temp[1]);
            imagejpeg($tmp, 100);

     // which i can`t fix

        }

    } else {
        $errors['file'] = 'The file is not an image. Valid file types: jpg, png, gif';
    }
}
}
  • 写回答

1条回答 默认 最新

  • ds3422222222 2016-11-21 02:30
    关注

    use these functions :

        #####  This function will proportionally resize image ##### 
        function do_resize_image($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality){
    
            if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize
    
            //do not resize if image is smaller than max size
            if($image_width <= $max_size && $image_height <= $max_size){
                if(save_image($source, $destination, $image_type, $quality)){
                    return true;
                }
            }
    
            //Construct a proportional size of new image
            $image_scale    = min($max_size/$image_width, $max_size/$image_height);
            $new_width      = ceil($image_scale * $image_width);
            $new_height     = ceil($image_scale * $image_height);
    
            $new_canvas     = imagecreatetruecolor( $new_width, $new_height ); //Create a new true color image
    
            //Copy and resize part of an image with resampling
            if(imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height)){
                save_image($new_canvas, $destination, $image_type, $quality); //save resized image
            }
    
            return true;
        }
    
    ##### Saves image resource to file ##### 
    function save_image($source, $destination, $image_type, $quality){
        switch(strtolower($image_type)){//determine mime type     
            case 'image/jpeg': case 'image/pjpeg': 
                imagejpeg($source, $destination, $quality); return true; //save jpeg file
                break;
            default: return false;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle