2012-09-10 48 views
2

我是一個php noob,並嘗試創建圖像大小調整腳本,可將上傳的圖像縮小爲更小的維度。使用下面的當前腳本,我可以看到顯示的原始圖像。新的寬度和高度似乎沒有註冊。任何幫助表示讚賞。針對不同MIME類型的PHP圖像大小調整

<?php 

    if ($_SERVER['REQUEST_METHOD']=='POST') { 
     if (isset($_FILES['image']['tmp_name'])) { 
     if ($_FILES['image']['error']==0){ 
     $new_dir = '/PHP_MySQL_Practice/uploaded/images'; 
     $fullpath = $_SERVER['DOCUMENT_ROOT'].$new_dir; 
     if(!is_dir($fullpath)) { 
      mkdir($fullpath, 0777, TRUE); 
     } 
     //get file name and type(extension) 
     $name = $_FILES['image']['name']; 
     $type = $_FILES['image']['type']; 

     //separate image name after first "dot" 
     $separated = explode(".", $name); 
     //get string before the dot which was stored as the 1st item in the separated array. 
     $first = $separated[0]; 
     //use php function pathinfo to get extension of image file 
     $ext = pathinfo($name, PATHINFO_EXTENSION); 
     //concatenate current timestamp (to avoid file overwrites) with the extracted file 'firstname' and add the extension 
     $name = time().'_'.$first.'.'.$ext; 
     $target = $fullpath.'/'.$name; 

     if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) { 

      resizeImage($target, $name); 
      echo '<img src="' . $target . '"/>'; 

     } 

    } 
    else { 

     echo 'there was an error in saving ur image file.'; 
    } 
} 
else { 

    echo 'ur image could not be uploaded.'; 
} 
    } 
    else { 
    ?> 
<form method="POST" enctype="multipart/form-data"> 
    <label>upload image: </label> 
    <input type="file" name="image" /> 
    <input type="submit" /> 
</form> 
    <?php 
    } 

    //function to resize image 
    function resizeImage($dir, $img) { 
list($src_w, $src_h) = getimagesize($dir); 
$max_w = 150; 
$max_h = 150; 
if ($src_w > $max_w) { 
    $ratio = $max_w/$src_w; 
    $new_img_w = $src_w * $ratio; 
} 
else if ($src_h > $max_h) { 
    $ratio = $max_h/$src_h; 
    $new_img_h = $src_h * $ratio; 
} 
$img_mime_type = getimagesize($dir); 
switch ($img_mime_type['mime']) { 
    case 'image/jpeg': 
    case 'image/pjpeg': 
     $src = imagecreatefromjpeg($dir); 
     return $src; 
     break; 
    case 'image/gif': 
     $src = imagecreatefromgif($dir); 
     return $src; 
     break; 
    case 'image/png': 
     $src = imagecreatefrompng($dir); 
     return $src; 
     break; 
    default: 
     return FALSE; 
     break; 
} 
$new_img = imagecreatetruecolor($new_img_w, $new_img_h); 
imagecopyresampled($new_img, $src, 0, 0, 0, 0, $new_img_w, $new_img_h, $max_w, $max_h); 
switch ($img_mime_type['mime']) { 
    case 'image/jpeg': 
    case 'image/pjpeg': 
     imagejpeg($new_img, $dir, 100); 
     break; 
    case 'image/gif': 
     imagegif($new_img, $dir); 
     break; 
    case 'image/png': 
     imagepng($new_img, $dir); 
     break; 
    default: 
     return FALSE; 
     break; 
} 

imagedestroy($src); 
imagedestroy($new_img); 
    } 
    ?> 
+0

看看給定的嘖嘖,希望這會幫助你,你想要http://blog.webtech11.com/2012/04/21/upload-and-resize-an-image-with-php.html –

回答

3

存在一定的缺陷存在

  1. imagegif僅2個參數$im和保存位置
  2. imagepng具有壓縮級別從0到9,而不是100
  3. 你是不是從保存圖像,nullimagejpeg($new_img, null, 100);應改爲$dir
  4. 你最後的功能是imagdestroy($src);那是錯的,應該是imagedestroy($src);

switch應該是:

switch ($img_mime_type['mime']) { 
    case 'image/jpeg': 
    case 'image/pjpeg': 
     imagejpeg($new_img, $dir, 100); 
     break; 
    case 'image/gif': 
     imagegif($new_img, $dir); 
     break; 
    case 'image/png': 
     imagepng($new_img, $dir); 
     break; 
    default: 
     return FALSE; 
     break; 
} 

您調整大小功能:以下功能

function resizeImage($dir, $img) { 
    //echo $dir; die; 
    list($src_w, $src_h) = getimagesize($dir); 
    $max_w = 150; 
    $max_h = 150; 
    if ($src_w > $max_w) { 
     $ratio = $max_w/$src_w; 
     $max_w = $src_w * $ratio; 
    } 
    else if ($src_h > $max_h) { 
     $ratio = $max_h/$src_h; 
     $max_h = $src_h * $ratio; 
    } 
    $img_mime_type = getimagesize($dir); 
    $src = imagecreatefromstring(file_get_contents($dir)); 
    $new_img = imagecreatetruecolor($max_w, $max_h); 
    imagecopyresampled($new_img, $src, 0, 0, 0, 0, $max_w, $max_h, $src_w, $src_h); 
    switch ($img_mime_type['mime']) { 
     case 'image/jpeg': 
     case 'image/pjpeg': 
      imagejpeg($new_img, $dir, 100); 
      break; 
     case 'image/gif': 
      imagegif($new_img, $dir); 
      break; 
     case 'image/png': 
      imagepng($new_img, $dir); 
      break; 
     default: 
      return FALSE; 
      break; 
    } 

    imagedestroy($src); 
    imagedestroy($new_img); 
} 
+0

與上述改變嘗試...仍然沒有! –

+0

整個switch語句可以用'imagecreatefromstring(file_get_contents($ dir))'代替,如果它是不支持的類型,則檢查false。 –

+0

@shakirahman有沒有錯誤?設置display_errors爲 –

0

您不是保存圖像,而是輸出圖像的內容。如果您查看了imagejpeg的文檔,您會看到第二個參數是要保存圖像的路徑。因此,將第二個參數(空)更改爲您想要保存調整大小的圖像的位置。

+0

好的, LEM我嘗試了一下。 –

0

用來調整圖像

function image_resize($image, $maxWidth, $maxHeight = null) 
{ 
     $file = $image; 
     $file_headers = @get_headers($file); 

     //validate file not exists 
     if(isset($file_headers[0]) && strpos($file_headers[0], '404')) { 
      return; 
     } 

    $arrayUploadPath = wp_upload_dir(); 
    $fileUploadSubDir = str_replace(basename($image),'', str_replace($arrayUploadPath['baseurl'], '', $image)); 
    $fileUploadDir = $arrayUploadPath['basedir'] . $fileUploadSubDir; 

    //validate sour ce file existance 
    if(!file_exists($fileUploadDir . basename($src))) return null; 

    $fileUploadUrl = $arrayUploadPath['baseurl'] . $fileUploadSubDir; 
    $srcFileName = $fileUploadDir . basename($image); 
    $srcSize = getimagesize($srcFileName); 
    $width = $srcSize[0]; 
    $height = $srcSize[1]; 

    if(empty($maxHeight)) { 
     $size = wp_constrain_dimensions($width, $height, $maxWidth); 
     $maxHeight = $size[1]; 
    } 

    //fix for image_resize_dimensions() constraint in image_resize() function 
    if($maxHeight >= $height) $maxHeight = $height - 1; 

    $newImage = image_resize($srcFileName, $maxWidth, $maxHeight); 

    $newFileNameUrl = !empty($newImage) && !is_object($newImage) ? $fileUploadUrl . basename($newImage) : null; 

    return $newFileNameUrl; 
} 
+0

我敢肯定,這些作品,可能會更優化,但我試圖學習PHP,因此試圖與我所知道的迄今。謝謝。 –

0

試試這個功能...

進入此功能,您可以通過高度和寬度按您的要求,因爲總共有三種可能性中,你可以按比例

  1. 如果調整圖像,高度大於寬度的,那麼你必須調整與高度。
  2. 如果寬度大於寬度,則必須調整寬度。
  3. 調整高度和寬度。

這裏是例子,

//first of all get Height and width of image 

function getHeight($image) { 
    $sizes = getimagesize($image); 
    $height = $sizes[1]; 
    return $height; 
} 

//You do not need to alter these functions 
function getWidth($image) { 
    $sizes = getimagesize($image); 
    $width = $sizes[0]; 
    return $width; 
} 

現在需要的尺寸在這裏我有500px的工作調整(高度和寬度)

$width = $this->getWidth($fullpath); 
    $height = $this->getHeight($fullpath); 

    $profilecroppedthumb = UPLOAD_PATH."FILENAME_WITH_EXTENSION"; 
    $fullpath = $uploadDirectory.$filename.'.'.$ext; 

     if ($width >= 500 && $height >= 500) { 
      thumb($fullpath, $profilecroppedthumb, "500", "500"); 
     } 
     elseif($width >= 500 && $height <= 500) 
     { 
      thumb($fullpath, $profilecroppedthumb, "500", ""); 
     } 
     elseif($width <= 500 && $height >= 500) 
     { 
      thumb($fullpath, $profilecroppedthumb, "", "500"); 
     } 
     else 
     { 
      $target = fopen($profilecroppedthumb, "w"); 
      fseek($temp, 0, SEEK_SET); 
      stream_copy_to_stream($temp, $target); 
      fclose($target); 
     } 
function thumb($file, $save, $width="", $height="") 
{ 
    //GD-Lib > 2.0 only! 
    //@unlink($save); 

    //get sizes else stop 

    if (!$infos = @getimagesize($file)) { 
    //echo $infos = @getimagesize($file); exit('saf'); 
     return false; 
    } 

    // keep proportions 
    $iWidth = $infos[0]; 
    $iHeight = $infos[1]; 
    $iRatioW = $width/$iWidth; 
    $iRatioH = $height/$iHeight; 


    if($height == "") { 
     if ($iRatioW < $iRatioH) { 
       $iNewW = $iWidth * $iRatioW; 
       $iNewH = $iHeight * $iRatioW; 
      } else { 
       $iNewW = $iWidth * $iRatioW; 
       $iNewH = $iHeight * $iRatioW; 
      } 
    } 
    elseif ($width == "") 
    { 
    if ($iRatioW < $iRatioH) { 
       $iNewW = $iWidth * $iRatioH; 
       $iNewH = $iHeight * $iRatioH; 
      } else { 
       $iNewW = $iWidth * $iRatioH; 
       $iNewH = $iHeight * $iRatioH; 
      } 
    } 
    else 
    { 
     if ($iRatioW < $iRatioH) { 
      $iNewW = $iWidth * $iRatioW; 
      $iNewH = $iHeight * $iRatioW; 
     } else { 

      $iNewW = $iWidth * $iRatioH; 
      $iNewH = $iHeight * $iRatioH; 


     } 
    } 
    //$iNewW = $width; 
    //$iNewH = $height; 

    //Don't resize images which are smaller than thumbs 
    if ($infos[0] < $width && $infos[1] < $height) { 
     $iNewW = $infos[0]; 
     $iNewH = $infos[1]; 
    } 

    if($infos[2] == 1) { 
     /* 
     * Image is typ gif 
     */ 
     $imgA = imagecreatefromgif($file); 
     $imgB = imagecreate($iNewW,$iNewH); 

     //keep gif transparent color if possible 
     if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) { 
      $transcolorindex = imagecolortransparent($imgA); 
       //transparent color exists 
       if($transcolorindex >= 0) { 
        $transcolor = imagecolorsforindex($imgA, $transcolorindex); 
        $transcolorindex = imagecolorallocate($imgB, $transcolor['red'], $transcolor['green'], $transcolor['blue']); 
        imagefill($imgB, 0, 0, $transcolorindex); 
        imagecolortransparent($imgB, $transcolorindex); 
       //fill white 
       } else { 
        $whitecolorindex = @imagecolorallocate($imgB, 255, 255, 255); 
        imagefill($imgB, 0, 0, $whitecolorindex); 
       } 
     //fill white 
     } else { 
      $whitecolorindex = imagecolorallocate($imgB, 255, 255, 255); 
      imagefill($imgB, 0, 0, $whitecolorindex); 
     } 
     imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]); 
     imagegif($imgB, $save);   

    } elseif($infos[2] == 2) { 
     /* 
     * Image is typ jpg 

     */ 
     $imgA = imagecreatefromjpeg($file); 
     $imgB = imagecreatetruecolor($iNewW,$iNewH); 
     imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]); 
     imagejpeg($imgB, $save,100); 

    } elseif($infos[2] == 3) { 
     /* 
     * Image is typ png 
     */ 
     $imgA = imagecreatefrompng($file); 
     $imgB = imagecreatetruecolor($iNewW, $iNewH); 
     imagealphablending($imgB, false); 
     imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]); 
     imagesavealpha($imgB, true); 
     imagepng($imgB, $save,100); 
    } else { 
     return false; 
    } 

    return true; 
} 

希望這有助於...