0

我正在尝试创建一个图像上传系统,但遇到了一个问题。系统上传插入的图像并创建缩略图。

允许用户提供缩略图的宽度和高度。当图像是 200px x 100px 并且用户说缩略图宽度为 10px 且高度为 20px 时,系统必须拉伸图像,这是我不希望发生的。我希望系统使用 Imagick 功能切掉图像的一部分,以便在图像通过脚本时获得正常的缩略图和图像。

该函数的使用如下:

chopImage ( width of the chopped area , height of the chopped area , x coordinate of topleft corner of chopped area , y coordinate of topleft corner of chopped area )

php上传系统标准配置:

$image = $_FILES["image"]["name"];
            $imgtemp = $_FILES["image"]["tmp_name"];
            $imgtype = $_FILES["image"]["type"];
            $imgsize = $_FILES["image"]["size"];    

if(!$image){
        if($image_enabled ==2){
            $errors[] = "You didn't selected an image to upload";
            }
        } else {
    if( $imgtype == 'image/jpeg' ){ $filetype= '.jpg'; }else{ $filetype= str_replace ( 'image/', '', $imgtype ); }

    $path= 'images/' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
    $thumb_path= 'images/thumb_' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
    $imgsize2= getimagesize( $imgtemp );
    $width= $imgsize2[0];
    $height= $imgsize2[1];

    $maxwidth= 1281;
    $maxheight= 721;
    $allowed= array( 'image/png', 'image/jpeg', 'image/gif', );

    if( in_array( $imgtype, $allowed ) ){

        if( $width < $maxwidth && $height < $maxheight ){

        if( $imgsize < 5242880 ){

我坚持的部分:

                    if(!isEmpty($image_thumbheight) || !isEmpty($image_thumbwidth)){

                        switch( $imgtype ){
                            case 'image/gif';
                            $img= imagecreatefromgif( $imgtemp );
                            $img_thumb= imagecreatefromgif( $thumb );
                        header("Content-type: image/gif");
        $image_crop = new Imagick("$image"); 
        $image_crop->cropImage( $image_thumbwidth,$image_thumbheight, 0,$height );
        $image_crop->writeImage($thumb);
        imagegif( $thumb, $thumb_path );
        break;

                        case 'image/png';
                            $img= imagecreatefrompng( $imgtemp );

                        header("Content-type: image/png");
        $image_crop = new Imagick("$imgtemp"); 
        $image_crop->chopImage( $image_thumbwidth,$image_thumbheight, 0,$height );
        $image_crop->writeImage($thumb);
        imagepng( $thumb, $thumb_path );
        break;
        case 'image/jpeg';
                            $img= imagecreatefromjpeg( $imgtemp );
                            $img_thumb= imagecreatefromjpeg( $thumb );
                        header("Content-type: image/jpeg");
        $image_crop = new Imagick("$image"); 
        $image_crop->chopImage( $image_thumbwidth,$image_thumbheight, 0,$height );
        $image_crop->writeImage($thumb_path);
        imagejpeg( $thumb, $thumb_path );
        break;
                        }

                move_uploaded_file( $imgtemp, $path );
                echo "Image is successfully uploaded.";
                        }

当我运行这个脚本时,它只上传普通图像。缩略图未上传。imagick 函数我估计不常使用,因为在网上找不到任何使用 imagick 函数的上传教程。有人可以帮我吗?

4

1 回答 1

0

您的代码看起来有点混乱……这是否按您的预期工作?

if(!isEmpty($image_thumbheight) || !isEmpty($image_thumbwidth)){
    $image_crop = new Imagick( $imgtmp );
    $image_crop->cropImage( $image_thumbwidth, $image_thumbheight, 0, $height );
    $image_crop->writeImage( $thumb_path );

    move_uploaded_file( $imgtemp, $path );
    echo "Image is successfully uploaded.";
}
于 2013-07-08T15:03:42.450 回答