0

尝试使用以下脚本通过表单上传、调整大小和保存多个图像。它不工作......甚至没有剥离到一个输入和 foreach 删除。我尝试组合上传和调整大小脚本,然后将表单操作重定向到组合脚本。不能让它工作。现在尝试使用 move_uploaded_file 将上传的文件从 upload.php 移动到 SimpleImage.php 但它似乎也不起作用。没有任何错误只是空白页。我稍后会添加更好的验证,只是试图让它现在工作。抱歉,代码这么多,只是想把所有的代码都放在这里供您参考。

我的问题:如何让 upload.php 将图像发送到调整大小脚本,反之亦然?关于它为什么不工作或显示错误的任何想法?

我是 php 新手,但我下定决心要解决这个问题!我会接受你可以对我提出的任何建议和/或侮辱!我向两者学习!:) 提前致谢!!!

表格:

<?php
   if( isset($_POST['submit']) ) {

      include('SimpleImage.php');
      $image = new SimpleImage();
      $image->load($_FILES['uploaded_image']['tmp_name']);
      $image->resizeToWidth(500);
      $image->save("uploads/uploadedfiles/".$pictures);
   } else {

?>

<form action="/upload.php" method="post" enctype="multipart/form-data"></br>

<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br /> 
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br /> 
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br /> 
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br /> 
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br /> 
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br />    <br />  
<input type="hidden" name="MAX_FILE_SIZE" value="48000000" /> *Images must be no more than     5MB.<br /> .jpg, .gif &amp; .png files accepted only.<br /><br />
<input type="submit" value="Upload"> </p> </form>

上传.php:

<?php


header("Location:confirmationpage.php");

foreach ($_FILES["pictures"]["error"] as $key => $error) {
  if (($_FILES["pictures"]["type"] == "image/gif")
  || ($_FILES["pictures"]["type"] == "image/jpeg")
  || ($_FILES["pictures"]["type"] == "image/png" )
  && ($_FILES["pictures"]["size"] < 50000))
   {
       move_uploaded_file(
         $_FILES["pictures"]["tmp_name"][$key],     "/SimpleImage.php" . $_FILES["pictures"]["name"][$key]); 
   }
}
 ?>

调整大小脚本(SimpleImage.php):

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {

   var $image;
   var $image_type;

   function load($uploaded_image) {

  $image_info = getimagesize($uploaded_image);
  $this->image_type = $image_info[2];
  if( $this->image_type == IMAGETYPE_JPEG ) {

     $this->image = imagecreatefromjpeg($uploaded_image);
  } elseif( $this->image_type == IMAGETYPE_GIF ) {

     $this->image = imagecreatefromgif($uploaded_image);
  } elseif( $this->image_type == IMAGETYPE_PNG ) {

     $this->image = imagecreatefrompng($uploaded_image);
  }
   }
   function save($uploaded_image, $image_type=IMAGETYPE_JPEG, $compression=75,     $permissions=null) {

  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image,$uploaded_image,$compression);
  } elseif( $image_type == IMAGETYPE_GIF ) {

     imagegif($this->image,$uploaded_image);
  } elseif( $image_type == IMAGETYPE_PNG ) {

     imagepng($this->image,$uploaded_image);
  }
  if( $permissions != null) {

     chmod($uploaded_image,$permissions);
  }
   }
   function output($image_type=IMAGETYPE_JPEG) {

  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image);
  } elseif( $image_type == IMAGETYPE_GIF ) {

     imagegif($this->image);
  } elseif( $image_type == IMAGETYPE_PNG ) {

     imagepng($this->image);
  }
   }
   function getWidth() {

  return imagesx($this->image);
   }
   function getHeight() {

  return imagesy($this->image);
   }
   function resizeToHeight($height) {

  $ratio = $height / $this->getHeight();
  $width = $this->getWidth() * $ratio;
  $this->resize($width,$height);
   }

   function resizeToWidth($width) {
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $this->resize($width,$height);
   }

   function scale($scale) {
  $width = $this->getWidth() * $scale/100;
  $height = $this->getheight() * $scale/100;
  $this->resize($width,$height);
   }

   function resize($width,$height) {
  $new_image = imagecreatetruecolor($width, $height);
  imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this-        >getWidth(), $this->getHeight()); 
  $this->image = $new_image;
   }      

}
?>
4

1 回答 1

2

好的,所以我们都是新的,新事物总是存在的。第一件事是将图像移动到 php 文件中的尝试......当你的脚本被调用时,它会被解析、编译和执行(基本上,但通常),所以这个将图像移动到脚本中到目前为止还行不通我认为。您需要编写一个脚本来实例化 simpleImage 类的实例并调用该类的方法(现在是对象),并将上传的图像作为参数传递给您要使用的方法。

实例化简单的图像...

  $imgWrk = new simpleImage;

使用简单的图像...

   $imgWrk->load($var)

所以这是第一点,您应该更加熟悉关于 simpleImage API(应用程序编程接口(在这种情况下接口就足够了))的细节。第二位是关于您将在其中实例化简单图像类的脚本。您需要将上传的文件移动到一个目录(假设您想要存储图像,无论您是否编辑它们),然后在这个脚本中您将编写类似 ownImageEdit.php(或其他任何内容)的标题,您将创建一个变量,其值将是此上传图像的句柄,并且将是您传递给 simpleImage 对象方法的变量。

现在我的回答有点笼统,因为对这个问题有价值的解决方案将涉及您最终必须做的代码重构,这涉及的内容比您意识到的要多,而且比这个问题的答案所允许的要多。

您需要了解 php 的 OOP,以及 php 脚本是如何执行的。学会爱上手册。

于 2012-12-06T23:50:18.533 回答