2014-10-31 15 views
0

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

?> 
+0

我已经发布你的问题的答案。我也创建了代码根据您的要求。 – 2014-10-31 07:09:18

回答

1

取出while语句 “而(($文件= READDIR($ DIR))!== FALSE){” 和相应的 “}”,并以$文件= “better.jpg” 代替

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: 
     } 
相关问题