проблема с классом загрузки изображений, не создающим img - PullRequest
1 голос
/ 28 января 2010

посмотрите на мой класс ниже, когда я пытаюсь загрузить изображение, которое по умолчанию продолжает вызываться в операторе switch. я не могу понять, почему это так.

<?php
class uploadimg2folder {



//create thumbnail
function createImage($fieldName,$width,$height) {

$thumb = $_FILES['$fieldName']['tmp_name'];
$path = "../uploads/".rand(0,1000);




    //verify file type and create an image resource
    switch($this->getImageExt($fieldName)) {

    case "jpg":
    $img = imagecreatefromjpeg($thumb);
    break;

    case "jpeg":
    $img = imagecreatefromjpeg($thumb);
    break;

    case "png":
    $img = imagecreatefrompng($thumb);
    break;


    case "gif":
    $img = imagecreatefromgif($thumb);
    break;

    case "bmp":
    $img = imagecreatefromwbmp($thumb);
    break;

    default: die("the file type you have upload is not supported by this application");

    }


    // Store image width and height
    list($img_width, $img_height) = getimagesize($img_src);


    // Create the new image
    $new_img = imagecreatetruecolor($width, $height);


    // Calculate stuff and resize image accordingly
    if (($width/$img_width) < ($height/$img_height)) {
        $new_width = $width;
        $new_height = ($width/$img_width) * $img_height;
        $new_x = 0;
        $new_y = ($height - $new_height) / 2;
    } else {
        $new_width = ($height/$img_height) * $img_width;
        $new_height = $height;
        $new_x = ($width - $new_width) / 2;
        $new_y = 0;
    }


    imagecopyresampled($new_img, $img, $new_x, $new_y, 0, 0, $new_width, $new_height, $img_width, $img_height);

    // Save thumbnail
    if (is_writeable(dirname($path))) {
        imagejpeg($new_img, $path, 100);
    }

    // Free up resources
    imagedestroy($new_img);
    imagedestroy($img);



}




    //this method gets the images extension
    function getImageExt($field_name) {

        $field = $_FILES['$field_name'][name];
        $field_ex = explode(".", $field);

        return end($field_ex);

    }




}
?>

так называется класс

if(isset($_POST['submit']) && $_FILES['image']['size'] > 0) {

$imgObj = new uploadimg2folder();
$imgObj->createImage('image','500','400');
}
?>

это форма на html-странице

<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
<label>image: </label>
<input type="file" name="image">
<br/>
<input type="submit" name="submit" value="upload">
</form>

Ответы [ 2 ]

1 голос
/ 28 января 2010

Сбой switch из-за проблем с функцией getImageExt, особенно в следующей строке:

    $field = $_FILES['$field_name'][name];
  • Это должно быть $_FILES["$field_name"] - переменные не интерполируются при использовании одинарных кавычек.
  • name должно быть 'name' .. эта строка выдаст предупреждение.
1 голос
/ 28 января 2010

Самый простой способ выяснить, что происходит, это повторить значение, подаваемое на ваш коммутатор. Просто добавьте

echo $this->getImageExt($fieldName);
...