如何使用PHP通过图像名称调整文件夹中的图像?

时间:2014-10-31 06:53:45

标签: php css image

如何使用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;
        }
    }
}

?>

3 个答案:

答案 0 :(得分:1)

删除while语句&#34; while(($ file = readdir($ dir))!== false){&#34;和相应的&#34;}&#34;并用$ file =&#34; better.jpg&#34;

替换它

答案 1 :(得分:0)

$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:
        }

答案 2 :(得分:-1)

我正在使用库jQuery文件上传https://github.com/blueimp/jQuery-File-Upload/wiki来处理图片 它有许多脚本类型:PHP,Python,Ruby ......

祝你好运