doubairan4213 2019-01-17 09:50
浏览 83
已采纳

图像未使用GD库正确调整大小

I'm developing a custom script to import photos from an FTP folder into Wordpress. Since I do not include wp-load due to specific customer requests, I can not use the various classes like $ wpdb, etc ...

So I had to create the whole script, but I found a problem on resizing the photos with watermark applied...

This script picks up the photos in a folder, moves the original photo in high quality to a hidden folder to the public, then later, creates the copies in low resolution and applies the watermark above.

This is the primary function that cycle each photo contained in a folder:

function generate_resized_image($path_to, $path_from, $image, $sell_media_dir){
    $upload_dir = wp_upload_dir();

    $all_size = array(
        '1' => array('width'=>150, 'height'=>150), //thumbnail
        '2' => array('width'=>620, 'height'=>357), //medium
        '3' => array('width'=>768, 'height'=>442), //medium_large
        '4' => array('width'=>100, 'height'=>70), //very-small
        '5' => array('width'=>300, 'height'=>200), //max-user-view
    );

    rename(str_replace(".jpg", ".JPG", $path_from), str_replace(".JPG", ".jpg", $path_to));
    $newname = explode(".", $image);

    foreach($all_size as $size){

        //$path_to_save = $sell_media_dir."/".$newname[0]."-".$size['width']."x".$size['height'].".jpg";
        $path_to_sell = $upload_dir['path']."/".$newname[0]."-".$size['width']."x".$size['height'].".jpg";

        $img = resize_image( str_replace( ".JPG", ".jpg", $path_to ), $size['width'], $size['height'], $cut=($size['width'] == $size['height']) ? true : false);
        imagejpeg($img, $path_to_sell, 10);

        echo 'Immagine creata: ' . $newname[0]."-".$size['width']."x".$size['height'].".jpg ". PHP_EOL;

        //rename($path_to_save, $path_to_sell);
    }

    $or_image = imagecreatefromjpeg( str_replace( ".JPG", ".jpg", $path_to ) );

    imagejpeg($or_image, $upload_dir['path']."/".str_replace(".JPG",".jpg",$image), 10);

    $img_size = getimagesize($upload_dir['path']."/".str_replace(".JPG",".jpg",$image));

    return array(
        'width' => $img_size[0],
        'height' => $img_size[1]
    );
}

This call a function resize_image that, in theory, resize the images by applying the watermark above.

function resize_image($file, $w, $h, $crop=FALSE) {

    $stamp = imagecreatefrompng('./wp-content/uploads/2018/11/spanshot_watermark.png');

    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }

    $src = imagecreatefromjpeg($file);

    $w = imagesx($src);
    $h = imagesy($src);

    $stamp = PNGResize($stamp, $w, $h);

    $sx = imagesx($stamp);
    $sy = imagesy($stamp);

    // $par1 = (imagesx($src)/2)-($sx/2);
    // $par2 = (imagesy($src)/2)-($sy/2);

    imagecopy($src, $stamp, 0, 0, 0, 0, imagesx($stamp), imagesy($stamp));

    echo "La lunghezza nuova è: " . $newwidth . PHP_EOL;
    echo "L'altezza nuova è: " . $newheight . PHP_EOL;

    $dst = imagecreatetruecolor($newwidth, $newheight);

    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $src;
}

Finally, this function calls PNGResize a resizing of the watermark while maintaining the transparency, which adapts the latter to the size of the image to be resized.

function PNGResize($image, $w, $h)
{
    $oldw = imagesx($image);
    $oldh = imagesy($image);
    $temp = imagecreatetruecolor($w, $h);

    imagealphablending( $temp, false );
    imagesavealpha( $temp, true );

    imagecopyresampled($temp, $image, 0, 0, 0, 0, $w, $h, $oldw, $oldh);

    return $temp;
}

All this works correctly, except for the resizing of moveable images, which keep the original dimensions indifferently from the parameters, lowering the quality.

These are the original images in the right folder: enter image description here

And these are the images, correctly moved, but not properly resized.

enter image description here

I do not understand why the images are not resized according to the past dimensions, can someone help me?

  • 写回答

1条回答 默认 最新

  • dtihe8614 2019-01-17 10:24
    关注

    In you resize_image function you are returning the $src which should change with $dst which is resized image.

    So in your function change return $src; line with return $dst; and it will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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