尝试使用以下脚本通过表单上传、调整大小和保存多个图像。它不工作......甚至没有剥离到一个输入和 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 & .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;
}
}
?>