我是 php 的新手,我需要有关以下问题的帮助。我想添加一个脚本,我可以在显示之前裁剪并重新调整上传图像的大小。但我不知道该怎么做,因为如果我添加一些东西,照片就会毁了。
以下是代码:
的index.php
<html>
<head></head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
FILE:
<input type="file" name="image">
<input type="submit" name="" value="Submit">
</form>
<?php
//connect to DB
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("company") or die(mysql_error());
//file properties
$file = &$_FILES['image']['tmp_name'];
if(!isset($file))
echo "Please select image";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE)
echo "That's not an image.";
else
{
if (!$insert = mysql_query("INSERT INTO store VALUES ('','$image_name','$image')"))
echo "Problem uploading...";
else
{
$lastid = mysql_insert_id();
echo "Image Uploaded <p> Your Image: </p> <img src=get.php?id=$lastid>";
}
}
}
?>
</body>
</html>
&#13;
get.php
<?php
//connect to DB
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("company") or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_array($image);
$image =$image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
&#13;
有什么建议吗?
答案 0 :(得分:0)
您是否尝试使用ImageMagick裁剪和调整图像大小? http://www.sitepoint.com/crop-and-resize-images-with-imagemagick/
<?php
$inFile = "test.jpg";
$outFile = "test-cropped.jpg";
$image = new Imagick($inFile);
$image->cropImage(400,400, 30,10);
$image->writeImage($outFile);
?>
答案 1 :(得分:0)
如果你有任何类型的图片上传系统,你可以使用wideimage裁剪图片,这样就有两个版本,一个小版本和原版。