簡體   English   中英

調整圖像滑塊大小

[英]Resize image slider

我一直在嘗試調整默認主題圖像滑塊的大小,但無法將窗口調整為特定大小。 誰能告訴我為調整滑塊的寬度和高度的大小,我需要對代碼進行哪些添加?

如果您想將圖像裁剪為所需的大小,請在functions.php文件中使用以下代碼

add_image_size( $name, $width, $height, $crop );

並獲得此圖像。

echo wp_get_attachment_image( $id, $name );

或者如果您想以合適的寬高比調整圖像大小,請使用此功能,可能對您有所幫助。

 function resize ($width, $height ){
    /* Get original image x y*/
    list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
    /* calculate new image size with ratio */
    $ratio = max( $width/$w, $height/$h);
    $h = ceil($height / $ratio);
    $x = ($w - $width / $ratio) / 2;
    $w = ceil($width / $ratio);
    /* new file name */
    $path = 'uploads/'.$width.'x'.$height.'_'.$_FILES['image']['name'];
    /* read binary data from image file */
    $imgString = file_get_contents($_FILES['image']['tmp_name']);
    /* create image from string */
    $image = imagecreatefromstring($imgString);
    $tmp = imagecreatetruecolor($width, $height);
    imagecopyresampled($tmp, $image, 0, 0, $x, 0, $width, $height, $w, $h);
    /* Save image */
    switch ($_FILES['image']['type']) {
        case 'image/jpeg':
            imagejpeg($tmp, $path, 100);
            break;
        case 'image/png':
            imagepng($tmp, $path, 0);
            break;
        case 'image/gif':
            imagegif($tmp, $path);
            break;
        default:
            exit;
            break;
    }
    return $path;
    /* cleanup memory */
    imagedestroy($image);
    imagedestroy($tmp);
}

我在我的項目中使用此功能調整圖像大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM