使用php和imagejpeg降低图像质量

时间:2014-10-24 08:30:43

标签: php wordpress

如何使用PHP降低图像质量?我一直在试用这段代码:

function compress_image($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);

    //save file
    imagejpeg($image, $destination_url, $quality);

    //return destination file
    return $destination_url;
}

if( isset( $_FILES['file'] ) ) :

    $wp_upload_directory = wp_upload_dir();  
    $path = $wp_upload_directory['basedir'].'/path';
    $fileName = $_FILES['file']['name'];
    $tempPath = $_FILES['file']['tmp_name'];

    $fullPath = $path . '/' . $fileName;

    $fileContent = file_get_contents($_FILES['file']['tmp_name']);

    $d = compress_image($tempPath, $fullPath, 0);

    if( file_exists( $d ) ) :

    $fullPath = make_unique( $d );

    $return = move_uploaded_file( $tempPath, $d );

    $fileName = str_replace( $path . '/', '', $d);

    die( $fileName );

    endif;
endif

但这不起作用?为什么?

0 个答案:

没有答案