2012-07-23 105 views
0

嗨我基於這個功能從我在網上找到的一個,並嘗試修改它爲我的PHP頁面上傳他們的圖標,但我想限制用戶上傳圖像應該只大小100x100px。嗯,我只是把它用這樣的:上傳前檢查圖像尺寸

uploadImage($id,$_FILES['upload']['name'],$_FILES['upload']['tmp_name']); 

這就是我所做的功能:

function uploadImage($new_name,$imagename,$tmp_name){ 

if($tmp_name!=null||$tmp_name!=""){ 
    list($width, $height, $type, $attr) = getimagesize($tmp_name); 
     if($width==100&&$height==100){ 
      $image1 = $imagename; 

      $extension = substr($image1, strrpos($image1, '.') + 1); 
      $image = "$new_name.$extension"; 
      $folder = "Images/"; 
        if($image) { 
        $filename = $folder.$image; 

        $copied = copy($tmp_name, $filename); 
        } 
        else echo "image not uploaded."; 
      } 
      else 
      echo "upload only 100x100px image!"; 
    } 

} 

現在的問題是,即使我上傳超過了100×100個像素尺寸的圖像仍然繼續沒有返回任何錯誤,現在我迷失了它。

回答

2

好吧,您也可以在上傳後調整圖片大小。

function createFixSizeImage($pathToImages, $pathToFixSizeImages, $Width) 
{ 

    // open the directory 
    $dir = opendir($pathToImages); 

    // loop through it, looking for any/all JPG files: 
    while (false !== ($fname = readdir($dir))) { 


    $image_info = getimagesize("path/to/images/".$fname); 
    $image_width = $image_info[0]; 
    $image_height = $image_info[1]; 
    $image_type = $image_info[2]; 


    switch ($image_type) 
    { 

    case IMAGETYPE_JPEG: 


    // parse path for the extension 
    $info = pathinfo($pathToImages . $fname); 
    // continue only if this is a JPEG image 
    if (strtolower($info['extension']) == 'jpeg') 
    { 

     // load image and get image size 
     $img = imagecreatefromjpeg("{$pathToImages}{$fname}"); 

     $width = imagesx($img); 
     $height = imagesy($img); 

     // give the size,u want 
     $new_width = 100; 
     $new_height = 100; 

     // create a new temporary image 
     $tmp_img = imagecreatetruecolor($new_width, $new_height); 

     // copy and resize old image into new image 
     imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

     // save Fix Size Images into a file 

     imagejpeg($tmp_img, "{$pathToFixSizeImages}{$fname}"); 

    } 
     break; 



    case IMAGETYPE_PNG: 
     // parse path for the extension 
    $info = pathinfo($pathToImages . $fname); 
    // continue only if this is a JPEG image 
    if (strtolower($info['extension']) == 'png') 
    { 

     // load image and get image size 
     $img = imagecreatefrompng("{$pathToImages}{$fname}"); 

     $width = imagesx($img); 
     $height = imagesy($img); 


     $new_width = 100; 
     $new_height = 100; 

     // create a new temporary image 
     $tmp_img = imagecreatetruecolor($new_width, $new_height); 

     // copy and resize old image into new image 
     imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

     // save Fix Size Images into a file 

     imagejpeg($tmp_img, "{$pathToFixSizeImages}{$fname}"); 

    } 
     break; 

    case IMAGETYPE_BMP: 
     echo "bmp"; 
     break; 



    default: 
     break; 
    } 
} 
    } 
    // close the directory 
    closedir($dir); 
} 

createFixSizeImage("path","path/to/images/to/be/saved",100); 
3

您可能需要重新考慮一下代碼;有一個功能,檢查上傳的圖像是否有效,然後實際上傳。或者,您可以創建一個班級。

<?php 

class ImageUpload 
{  
    public $tmpImage; 
    public $maxWidth = 100; 
    public $maxHeight = 100; 
    public $errors = []; 

    public function __construct($image) 
    { 
     $this->tmpImage = $image; 
    } 

    public function upload() 
    { 
     // Check image is valid; if not throw exception 

     // Check image is within desired dimensions 
     list($width, $height) = getimagesize($this->tmpImage); 

     if ($width > $this->maxWidth || $height > $this->maxHeight) { 
      throw new Exception(sprintf('Your image exceeded the maximum dimensions (%d&times;%d)', $this->maxWidth, $this->maxHeight)); 
     } 

     // Create filename 
     // Do the upload logic, i.e. move_uploaded_file() 
    } 
} 

然後,您可以使用這個類,如下所示:

<?php 

$imageUpload = new ImageUpload($_FILES['upload']['tmp_name']); 

try { 
    $imageUpload->upload(); 
} catch (Exception $e) { 
    echo 'An error occurred: ' . $e->getMessage(); 
} 

這被註銷的袖口,所以他們可能是錯誤的。但希望它能夠演示處理文件上傳的更好方法,以及在上傳過程中可能發生的錯誤。

1

擴展或多或少未知的代碼,然後調試它,就像你幾周前寫了一些代碼,你不再理解它。

在你的情況下,你正在擴展一些現有的代碼(你沒有發佈原始代碼,但你寫道,你這樣做)通過添加檢查圖像大小的功能。

,這樣你就不需要編輯多的(不明,但)工作代碼,創建新的功能,因爲它本身的功能:

/** 
* @param string $file 
* @param int $with 
* @param int $height 
* @return bool|null true/false if image has that exact size, null on error. 
*/ 
function image_has_size($file, $width, $height) 
{ 
    $result = getimagesize($file); 
    if ($count($result) < 2) { 
     return null; 
    } 

    list($file_width, $file_height) = $result; 

    return ($file_width == (int) $width) 
      && ($file_height == (int) $height); 
} 

您現在可以在一個單一的功能,新功能你可以更容易地集成到原來的代碼(希望工作)。

用法:

$imageHasCorrectSize = image_has_size($tmp_name, 100, 100); 

所以每當你更改代碼,做這樣一個外科醫生,保持切口儘可能小。