doucheng8471 2017-10-11 06:36
浏览 101
已采纳

使用PHP类上传多个图像

I need to inquire that i am using PHP class to upload single image. below I have the class and PHP code to upload image. it is working fine for single image upload, and Now I want to upload multiple image using the same class, I used for loop for this purpose and it gives some error. please brief where i am doing wrong

HTML for multiple images

<input type="file" name="photo[]" placeholder="Photo">

code for multiple images

for($i=0;$i<$count;$i++ ) {
        if(isset($_FILES['photo']['tmp_name'][$i]) && ($_FILES['photo']['tmp_name'][$i]!="")){
            $uploadImage = new UploadImage;
            echo $uploadImage->upload('photo', null, '../uploads/', 150, 0, '../uploads/thumb/', 75, 75);
    }
          }

Below code is for single image upload which is working fine.

HTML

<input type="file" name="photo" placeholder="Photo">

code

if(isset($_FILES['photo']['tmp_name']) && ($_FILES['photo']['tmp_name']!="")){
            $uploadImage = new UploadImage;
            $obj['image'] = $uploadImage->upload('photo', null, '../uploads/', 150, 0, '../uploads/thumb/', 75, 75);
        }

Class.php

<?php
class UploadImage {
    public function upload($imageField, $imageFieldIndex = null, $strLargePath = null, $largeWidth = 0, $largeHeight = 0, $strThumbPath = null, $thumbWidth = 0, $thumbHeight = 0)
    {
        $noLarge = false;
        $noThumb = false;

        if(empty($strLargePath)) {
            $noLarge = true;
        }

        if(empty($strThumbPath)) {
            $noThumb = true;
        }


    echo   $fileName = isset($imageFieldIndex) ? stripslashes($_FILES[$imageField]['name'][$imageFieldIndex]) : stripslashes($_FILES[$imageField]['name']);
       $fileTempName = isset($imageFieldIndex) ? $_FILES[$imageField]['tmp_name'][$imageFieldIndex] : $_FILES[$imageField]['tmp_name'];

        $extension = $this->getExtension($fileName);

        $imageName = time().$imageFieldIndex.'.'.$extension;

        if($noLarge == false) {
            $this->resize($largeWidth, $largeHeight, $fileTempName, $imageName, $strLargePath);
        }

        if($noThumb == false) {
            $this->resize($thumbWidth, $thumbHeight,$fileTempName, $imageName, $strThumbPath);
        }
        return $imageName;
    }

    private function getExtension($strInput) 
    {
        $i = strrpos($strInput,".");
        if (!$i){return null;}

        $j = strlen($strInput) - $i;
        $output = substr($strInput, $i + 1, $j);
        return $output;
    }

    private function resize($newWidth, $newHeight, $imageTempName, $imageName, $savePath) {
        $image = new ResizeImage;
        $image->newWidth = $newWidth;
        $image->newHeight = $newHeight;

        $image->imageTempName = $imageTempName; // Full Path to the file

        $image->ratio = true; // Keep Aspect Ratio?

        // Name of the new image (optional) - If it's not set a new will be added automatically

        $image->imageName = substr($imageName, 0, strrpos($imageName, '.'));

        /* Path where the new image should be saved. If it's not set the script will output the image without saving it */

        $image->savePath = $savePath;

        $process = $image->resize();

        if($process['result'] && $image->savePath)
        {
            //echo 'The new image ('.$process['new_file_path'].') has been saved.';
        }
    }
}

/*-------------------------------- Image resize Class -----------------------------------------*/
class ResizeImage {

    var $imageTempName;
    var $newWidth;
    var $newHeight;
    var $ratio;
    var $imageName;
    var $savePath;

    function resize(){
        if(!file_exists($this->imageTempName)){
          exit("File ".$this->imageTempName." does not exist.");
        }

        $info = GetImageSize($this->imageTempName);

        if(empty($info)){
          exit("The file ".$this->imageTempName." doesn't seem to be an image.");   
        }

        $width = $info[0];
        $height = $info[1];
        $mime = $info['mime'];

        /* Keep Aspect Ratio? */
        $this->ratio = true;
        if($this->ratio){
            $thumb = ($this->newWidth < $width && $this->newHeight < $height) ? true : false; // Thumbnail
            $largeImage = ($this->newWidth >= $width || $this->newHeight >= $height) ? true : false; // Large Image
            if($thumb){
                if($this->newWidth > $this->newHeight){
                    $x = ($width / $this->newWidth);
                    $this->newHeight = ($height / $x);
                }else {
                    $x = ($height / $this->newHeight);
                    $this->newWidth = ($width / $x);
                }
            }else if($largeImage){
                if($this->newWidth >= $width){
                    $x = ($this->newWidth / $width);
                    $this->newHeight = ($height * $x);
                }
                else if($this->newHeight >= $height){
                    $x = ($this->newHeight / $height);
                    $this->newWidth = ($width * $x);
                }
            }
        }

        // What sort of image?

        $type = substr(strrchr($mime, '/'), 1);

        switch ($type){
            case 'jpeg':
                $image_create_func = 'ImageCreateFromJPEG';
                $image_save_func = 'ImageJPEG';
                $newImageExt = 'jpg';
                break;

            case 'jpg':
                $image_create_func = 'ImageCreateFromJPEG';
                $image_save_func = 'ImageJPEG';
                $newImageExt = 'jpg';
                break;

            case 'png':
                $image_create_func = 'ImageCreateFromPNG';
                $image_save_func = 'ImagePNG';
                $newImageExt = 'png';
                break;

            case 'bmp':
                $image_create_func = 'ImageCreateFromBMP';
                $image_save_func = 'ImageBMP';
                $newImageExt = 'bmp';
                break;

            case 'gif':
                $image_create_func = 'ImageCreateFromGIF';
                $image_save_func = 'ImageGIF';
                $newImageExt = 'gif';
                break;

            case 'vnd.wap.wbmp':
                $image_create_func = 'ImageCreateFromWBMP';
                $image_save_func = 'ImageWBMP';
                $newImageExt = 'bmp';
                break;

            case 'xbm':
                $image_create_func = 'ImageCreateFromXBM';
                $image_save_func = 'ImageXBM';
                $newImageExt = 'xbm';
                break;

            default: 
                $image_create_func = 'ImageCreateFromJPEG';  
                $image_save_func = 'ImageJPEG';
                $newImageExt = 'jpg';
            }
            // New Image
            $image_c = imagecreatetruecolor($this->newWidth, $this->newHeight);
            $newImage = $image_create_func($this->imageTempName);
            imagealphablending($image_c, false);
            imagesavealpha($image_c,true);
            $transparent = imagecolorallocatealpha($image_c, 255, 255, 255, 127);
            imagefilledrectangle($image_c, 0, 0, $this->newWidth, $this->newHeight, $transparent);

            ImageCopyResampled($image_c, $newImage, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $width, $height);

                if($this->savePath)
                {
                   if($this->imageName)
                   {
                    $new_name = $this->imageName.'.'.$newImageExt;
                   }
                   else
                   {
                    $new_name = $this->newImageName(basename($this->imageTempName)).'_resized.'.$newImageExt;
                   }

                $save_path = $this->savePath.$new_name;
                }
                else
                {
                /* Show the image without saving it to a folder */
                   header("Content-Type: ".$mime);

                   $image_save_func($image_c);

                   $save_path = '';
                }

                $process = $image_save_func($image_c, $save_path);

                return array('result' => $process, 'new_file_path' => $save_path);

            }

        function newImageName($filename)
        {
            $string = trim($filename);
            $string = strtolower($string);
            $string = trim(ereg_replace("[^ A-Za-z0-9_]", " ", $string));
            $string = ereg_replace("[ \t
]+", "_", $string);
            $string = str_replace(" ", '_', $string);
            $string = ereg_replace("[ _]+", "_", $string);

            return $string;
        }
}
?>
  • 写回答

2条回答 默认 最新

  • duanjitong7226 2017-10-11 06:45
    关注

    Add the image index to the upload function

    for($i=0;$i<$count;$i++ ) {
            if(isset($_FILES['photo']['tmp_name'][$i]) && ($_FILES['photo']['tmp_name'][$i]!="")){
                $uploadImage = new UploadImage;
                echo $uploadImage->upload('photo', $i, '../uploads/', 150, 0, '../uploads/thumb/', 75, 75);
               //...................................^ here
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效