图片上传类不创建img的问题

发布于 2024-08-19 09:30:20 字数 2473 浏览 4 评论 0原文

当我尝试上传图像时,看看下面的类,默认值在 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>

take a look at my class below when i try to upload an image the default keeps being called within the switch statement. i cant see why it goes to that.

<?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);

    }




}
?>

this is how the class is being called

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

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

this is the form within the html page

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-08-26 09:30:20

弄清楚发生了什么的最简单方法是回显输入到交换机的值。只需添加

echo $this->getImageExt($fieldName);

The easiest way to figure out what is going on is echoing the value fed to your switch. Simply add

echo $this->getImageExt($fieldName);
深居我梦 2024-08-26 09:30:20

switch 失败,因为 getImageExt 函数存在问题,特别是以下行:

    $field = $_FILES['$field_name'][name];
  • This should be $_FILES["$field_name"] - 使用单引号时不会插入变量。
  • name 应该是 'name' .. 此行将生成警告。

The switch is failing because there are problems with the getImageExt function, particularly the following line:

    $field = $_FILES['$field_name'][name];
  • This should be $_FILES["$field_name"] - variables are not interpolated when using single quotes.
  • name should be 'name' .. this line will generate a warning.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文