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.

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

报告相同问题?

悬赏问题

  • ¥100 华为ensp只要2-9实验运行结果
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题