簡體   English   中英

如何使用php通過圖像名稱調整文件夾中圖像的大小?

[英]How to resize image in a folder by image name using php?

如何使用php通過圖像名稱調整文件夾中圖像的大小?

................................................... ................................................... ................................................... ......

我使用此代碼,但是它將調整文件夾中所有圖像的大小,但是我只想調整圖像名稱的大小: better.jpg我該怎么做?

<?php
//Maximize script execution time
ini_set('max_execution_time', 0);

//Initial settings, Just specify Source and Destination Image folder.
$ImagesDirectory    = '/home/public_html/websites/images/'; //Source Image Directory End with Slash
$DestImagesDirectory    = '/home/public_html/websites/images/new/'; //Destination Image Directory End with Slash
$NewImageWidth      = 500; //New Width of Image
$NewImageHeight     = 500; // New Height of Image
$Quality        = 80; //Image Quality

//Open Source Image directory, loop through each Image and resize it.
if($dir = opendir($ImagesDirectory)){
    while(($file = readdir($dir))!== false){

        $imagePath = $ImagesDirectory.$file;
        $destPath = $DestImagesDirectory.$file;
        $checkValidImage = @getimagesize($imagePath);

        if(file_exists($imagePath) && $checkValidImage) //Continue only if 2 given parameters are true
        {
            //Image looks valid, resize.
            if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
            {
                echo $file.' resize Success!<br />';
                /*
                Now Image is resized, may be save information in database?
                */

            }else{
                echo $file.' resize Failed!<br />';
            }
        }
    }
    closedir($dir);
}

//Function that resizes image.
function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
{
    list($iWidth,$iHeight,$type)    = getimagesize($SrcImage);
    $ImageScale             = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
    $NewWidth               = ceil($ImageScale*$iWidth);
    $NewHeight              = ceil($ImageScale*$iHeight);
    $NewCanves              = imagecreatetruecolor($NewWidth, $NewHeight);

    switch(strtolower(image_type_to_mime_type($type)))
    {
        case 'image/jpeg':
        case 'image/png':
        case 'image/gif':
            $NewImage = imagecreatefromjpeg($SrcImage);
            break;
        default:
            return false;
    }

    // Resize Image
    if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
    {
        // copy file
        if(imagejpeg($NewCanves,$DestImage,$Quality))
        {
            imagedestroy($NewCanves);
            return true;
        }
    }
}

?>

刪除while語句“ while(($ file = readdir($ dir))!== false){”和相應的“}”,然后將其替換為$ file =“ better.jpg”

$image      = 'better.jpg';

$imageExt = pathinfo($image, PATHINFO_EXTENSION);

// You can set below variables for the resize image.

//$newWidth        = New width of image
//$newHeight       = New Height of image
//$width           = Old Width
//$height          = Old Height
//$imagepath       = Directory to store image
//$imageName       = New Image Name
//$quality         = Image Quality

switch ($imageExt) {
            case "png":
                $image_p = imagecreatetruecolor($width, $height);
                $image   = imagecreatefrompng($image);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0,$newWidth, $newHeight, $width, $height);
                imagepng($image_p, $imagepath."/".$imageName, 9);
                break;
            case "gif":
                $image_p = imagecreatetruecolor($width, $height);
                $image   = imagecreatefrompng($image);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
                imagegif($image_p, $imagepath."/".$imageName);
                break;
            case "jpg":
                $image_p = imagecreatetruecolor($width, $height);
                $image   = imagecreatefromjpeg($image);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0,$newWidth, $newHeight, $width, $height);
                imagejpeg($image_p, $imagepath."/".$imageName, $quality);
                break;
            default:
        }

我正在使用libray jQuery File Upload https://github.com/blueimp/jQuery-File-Upload/wiki處理圖像它有很多腳本類型:PHP,Python,Ruby ...

祝好運

暫無
暫無

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

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