php图片大小调整不适用于图像上载
我正在尝试调整上载到php表单上的图像的大小。正在保存图片,但未调整大小。基本上,除了图像部分的大小调整外,一切正常。我把代码放在下面 带有php的html表单,用于重新调整大小php图片大小调整不适用于图像上载,php,image,forms,upload,resize,Php,Image,Forms,Upload,Resize,我正在尝试调整上载到php表单上的图像的大小。正在保存图片,但未调整大小。基本上,除了图像部分的大小调整外,一切正常。我把代码放在下面 带有php的html表单,用于重新调整大小 <?php include "base.php"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> &
<?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!");
}
?>
乍一看,您的图像大小调整代码位于if(isset($\u POST['submit'])中{
检查您的表单,但是当表单提交到submitAlbumForm.php
这是一个完全不同的页面时,此代码将永远不会运行。我认为您只需要将调整大小的代码移动到submitAlbumForm.php,可能在if(move_uploaded_文件(…)中
根据需要阻止并更改路径。我看不出if如何阻止if(isset($\u POST['submit']))
一旦被点击,如果被点击,图像数据将直接输出,这将不起作用,因为它被HTML包围。一个单独的脚本输出大小调整后的图像的数据,将由一个单独的脚本更合适地处理。(可能是一个以GET params作为大小的脚本?)如果您试图保存图像的已调整大小的副本而不是原始大小的图像,那么您可能应该在move\u uploaded\u file
之后执行此操作。您可能可以使用SimpleImage::save()
来执行此操作。这可能会起作用:
if(move_uploaded_file($_FILES['albumCover']['tmp_name'], $target))
{
require_once 'SimpleImage.php';
$image = new SimpleImage();
$image->load($target);
$image->resize(150,150);
$image->save($target); // Overwrites the image with a resized one
// 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 ...
你可以试试我自己的程序。
我上传和存储的数据和图像的大小可以减少,我们可以改变
我使用php和xampp。
目录:
C:\xampp\htdocs\phpyan\6_Database_2
6_Database_2 :
..images (dir)
..packages (dir)
..input_personal_info.php (file)
..images
..small(dir)
..more pict (file picture)
-----------------------------
..packages
..ClassDatabase.php
..Personal.php
“图像文件夹”中有一个名为“小”的文件夹
注:
在package文件夹中,我保存了一些类,它们可以作为存储数据和上传小尺寸图像的功能
输入_personal_info.php
<?php
include 'packages/ClassDatabase.php';
include 'packages/Personal.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Input Personal Information</title>
</head>
<body>
<h1>Personal Information</h1>
<form enctype="multipart/form-data" action="" name="frm_personal_info" method="post">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="First_Name"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="Last_Name"></td>
</tr>
<tr>
<td>Date Birth</td>
<td><input type="text" name="Date_Birth"></td>
</tr>
<tr>
<td>Gender</td>
<td>
<select name="Gender">
<option value="">Gender...</option>
<option value="Pria">Pria</option>
<option value="Wanita">Wanita</option>
<option value="Lainnya">Lain..</option>
</select>
</td>
</tr>
<tr>
<td>Photo Profile</td>
<td><input type="file" name="Photo_Profile"></td>
</tr>
<tr>
<td>About</td>
<td><textarea name="About" rows="4" cols="30"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
// connect with database
$connector = new ClassDatabase("localhost","root","","RawnayDB");
$connector->connect();
if (isset($_POST['submit'])) {
// assign values on variabel
$first_name = $_POST['First_Name'];
$last_name = $_POST['Last_Name'];
$date_birth = $_POST['Date_Birth'];
$gender = $_POST['Gender'];
$about = $_POST['About'];
$dir = "images/";
$dir2 = "images/small/";
$photo_name = $_FILES['Photo_Profile']['name'];
$photo_error = $_FILES['Photo_Profile']['error'];
$photo_type = $_FILES['Photo_Profile']['type'];
$photo_size = $_FILES['Photo_Profile']['size'];
$photo_tmp = $_FILES['Photo_Profile']['tmp_name'];
$personals = new Personal();
// save file foto
$personals->uploadPic($dir,$photo_name,$photo_tmp);
// save pic small
$personals->thumbPic($dir2,$photo_name);
// save data
$personals->savePersData($first_name,$last_name,$date_birth,$gender,$photo_name,$about);
}
?>
输入个人资料
个人信息
名字
姓
出生日期
性别
性别。。。
普里亚
丸田
躺在。。
照片简介
关于
packages/ClassDatabase.php
<?php
// class Database
class ClassDatabase{
var $host;
var $user;
var $pswd;
var $db;
function ClassDatabase($host,$user,$pswd,$db){
$this->host = $host;
$this->user = $user;
$this->pswd = $pswd;
$this->db = $db;
}
function connect(){
// Connection Test
if(mysql_connect($this->host,$this->user,$this->pswd))
{mysql_select_db($this->db);} else {echo "Connection Failed";}
}
}
?>
packages/Personal.php//>类
<?php
// class personal
class Personal{
var $tabel = "personal";
var $photo_original;
var $photo_small;
var $width;
var $height;
var $thumbWidth = 100;
var $thumbHeight;
function savePersData($fstnm,$lstnm,$dtbrth,$gndr,$photo_name,$about){
$small = $this->photo_small;
$original = $this->photo_original;
$photo_name = "Pic".$fstnm;
$query = "INSERT INTO $this->tabel VALUES('','$fstnm','$lstnm','$dtbrth','$gndr','$photo_name','$small','$original','$about');";
if(mysql_query($query)){
echo "<script>alert('penyimpanan sukses');</script>";
}
}
function uploadPic($dir,$photo_name,$photo_tmp){
is_uploaded_file($photo_tmp);
$check = move_uploaded_file($photo_tmp,$dir.$photo_name);
if($check){
$this->photo_original=$dir.$photo_name;
}
}
function thumbPic($dir2,$filename){
$photo_original = imagecreatefromjpeg($this->photo_original);
$this->width = imagesx($photo_original);
$this->height = imagesy($photo_original);
$this->thumbHeight = ($this->thumbWidth/$this->width)*$this->height;
// resample picture
$gbr_thumb = imagecreatetruecolor($this->thumbWidth,$this->thumbHeight);
imagecopyresampled($gbr_thumb,$photo_original,0,0,0,0,$this->thumbWidth,$this->thumbHeight,$this->width,$this->height);
// Save your directory
$acak = rand(00000,999999);
imagejpeg($gbr_thumb, $dir2."Small_Rawnay_Pic_".$acak.".jpeg");
$this->photo_small = $dir2."Small_Rawnay_Pic_".$acak.".jpeg";
}
}
?>
那么..你希望我们阅读所有这些内容吗?到底哪里出错了?这会更有帮助;)Stackoverflow不是一种调试服务。你必须问一个与编程相关的特定问题,向我们展示你代码的相关部分(而不是数百行)抱歉,我想我不是很清楚哈哈,除了html表单中的php部分,其他一切都正常工作,在上传到我的数据库之前,它实际上没有调整图像大小和保存图像。哪些代码部分是submitAlbumForm.php?你得到的php错误是什么?没错,就是这样。不仅仅是在,但他引用了$\u文件['albumCover']['name']
也在该页面上,因此他正在从请求中重新提取整个图像,即使它已被调整大小并保存在某个地方。非常感谢您的回复!确实帮了我很多忙!就是这样!非常感谢您的帮助!太好了!我没有检查代码,所以我很幸运。很高兴它帮了我的忙。
<?php
// class personal
class Personal{
var $tabel = "personal";
var $photo_original;
var $photo_small;
var $width;
var $height;
var $thumbWidth = 100;
var $thumbHeight;
function savePersData($fstnm,$lstnm,$dtbrth,$gndr,$photo_name,$about){
$small = $this->photo_small;
$original = $this->photo_original;
$photo_name = "Pic".$fstnm;
$query = "INSERT INTO $this->tabel VALUES('','$fstnm','$lstnm','$dtbrth','$gndr','$photo_name','$small','$original','$about');";
if(mysql_query($query)){
echo "<script>alert('penyimpanan sukses');</script>";
}
}
function uploadPic($dir,$photo_name,$photo_tmp){
is_uploaded_file($photo_tmp);
$check = move_uploaded_file($photo_tmp,$dir.$photo_name);
if($check){
$this->photo_original=$dir.$photo_name;
}
}
function thumbPic($dir2,$filename){
$photo_original = imagecreatefromjpeg($this->photo_original);
$this->width = imagesx($photo_original);
$this->height = imagesy($photo_original);
$this->thumbHeight = ($this->thumbWidth/$this->width)*$this->height;
// resample picture
$gbr_thumb = imagecreatetruecolor($this->thumbWidth,$this->thumbHeight);
imagecopyresampled($gbr_thumb,$photo_original,0,0,0,0,$this->thumbWidth,$this->thumbHeight,$this->width,$this->height);
// Save your directory
$acak = rand(00000,999999);
imagejpeg($gbr_thumb, $dir2."Small_Rawnay_Pic_".$acak.".jpeg");
$this->photo_small = $dir2."Small_Rawnay_Pic_".$acak.".jpeg";
}
}
?>