我想调整一个图像上传到我的PHP窗体上。图片正在保存,但没有调整大小。基本上,除了图像部分的大小调整以外,一切工作都很好。我把我的代码放在下面。php图片调整大小不能在图像上传
HTML表单与PHP重新上浆
<?php include "base.php"; ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Submit an Album</title>
</head>
<body>
<?php
if(isset($_POST['submit'])) {
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($_FILES['albumCover']['tmp_name']);
$image->resize(150,150);
$image->output();
} else {
?>
<table>
<tr>
<td align="center">Submit an Album</td>
</tr>
<tr>
<td>
<table>
<form enctype="multipart/form-data" action="submitAlbumForm.php" method="post">
<tr>
<td>Artist Name</td>
<td><input type="text" name="artistName" size="20">
</td>
</tr>
<tr>
<td>Album Name</td>
<td><input type="text" name="albumName" size="20">
</td>
</tr>
<tr>
<tr>
<td>Release Date</td>
<td><input type="text" name="releaseDate" size="20">
</td>
</tr>
<tr>
<tr>
<td>Leak Date</td>
<td><input type="text" name="leakDate" size="20">
</td>
</tr>
<tr>
<tr>
<td>Where It Leaked</td>
<td><input type="text" name="whereItLeaked" size="20">
</td>
</tr>
<tr>
<tr>
<td>Album Cover</td>
<td><input type="file" name="albumCover">
</td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" name="submit" value="Add"></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
<?php
}
?>
</body>
</html>
submitAlbumForm.php,php的形式上传表单信息数据库
<?php
include "base.php";
//Setting up images directory
$target = "images/";
$target = $target . basename($_FILES['albumCover']['name']);
$albumCover=($_FILES['albumCover']['name']);
//inserting data order
$order = "INSERT INTO albums
(artistName, albumName, releaseDate, leakDate, whereItLeaked, albumCover)
VALUES
('$_POST[artistName]',
'$_POST[albumName]',
'$_POST[releaseDate]',
'$_POST[leakDate]',
'$_POST[whereItLeaked]',
'{$_FILES['albumCover']['name']}')";
//Writes image to database
mysql_query("INSERT INTO `albums` VALUES ('albumCover')") ;
if(move_uploaded_file($_FILES['albumCover']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives an error if its not
echo "Sorry, there was a problem uploading your file.";
}
//declare in the order variable
$result = mysql_query($order); //order executes
if($result){
echo("<br>Thank you for submitting!");
} else{
echo("<br>Sorry, something went wrong! Please try again!");
}
?>
SimpleImage.php,脚本我使用调整图像 -
<?php
/*
* 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($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if($this->image_type == IMAGETYPE_JPEG) {
$this->image = imagecreatefromjpeg($filename);
} elseif($this->image_type == IMAGETYPE_GIF) {
$this->image = imagecreatefromgif($filename);
} elseif($this->image_type == IMAGETYPE_PNG) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if($image_type == IMAGETYPE_JPEG) {
imagejpeg($this->image,$filename,$compression);
} elseif($image_type == IMAGETYPE_GIF) {
imagegif($this->image,$filename);
} elseif($image_type == IMAGETYPE_PNG) {
imagepng($this->image,$filename);
}
if($permissions != null) {
chmod($filename,$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;
}
}
?>
我知道我现在有很多安全漏洞,并将工作在那些很快!多谢你们!
所以..您是希望我们阅读所有?在哪里_exactly_出错了?这会更有帮助;) – Bono 2012-08-03 17:58:46
Stackoverflow不是一个调试服务。您必须提出与编程有关的具体问题,向我们展示**代码的相关**部分(而不是数百行),并且有人可以为您提供帮助。 – Jocelyn 2012-08-03 18:02:35
对不起,我想我不是很清楚哈哈,除了在html表格中的php片段,一切都工作正常,它实际上并没有调整大小和保存图像,然后将其上传到我的数据库。 – user1570257 2012-08-03 18:02:54