谁能告诉我如何解决以下问题:
include('php/resizeImage.php');
if($_POST['uploadlink']){
$url = $_POST['uploadlink'];
$urlImage = file_get_contents($url);
if ($_POST['filename']){
$filename = $_POST['filename'].".jpg";
} else {
$urlinfo = parse_url($url);
$filename = basename($urlinfo['path']);
}
$image = new ResizeImage();
$image->load($filename);
$image->resizeToWidth(300);
$image->save($filename);
file_put_contents("images/upload/".$filename, $urlImage);
}
在我从 URL 的 file_get_contents 接收到图像数据后,我想通过我的 resizeImage 脚本调整它的大小,该脚本将图像的文件名作为参数。
编辑:ResizeImage 函数加载和 resizeToWidth:
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 resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
当用户通过 input type='file' 选择本地镜像时,我可以毫不费力地做到这一点。
if (isset($_FILES["uploadedfile"])){
$ufilename = $_FILES["uploadedfile"]["name"];
$ufiletmpname = $_FILES["uploadedfile"]["tmp_name"];
$image = new ResizeImage();
$image->load($ufiletmpname);
$image->resizeToWidth(300);
$image->save($ufiletmpname);
}
另一个问题: 我将用户名转发到我的脚本中,因为我想为每个用户创建一个单独的文件夹,这样他们只能看到自己上传的图像。
$admin = $_GET['admin'];
file_put_contents("images/upload/".$admin."/".$filename, $urlImage);
为什么这对我不起作用?
谢谢。
最佳答案
简单。
只需更改您的 ResizeImage 类的代码,使其能够操作除文件名之外的图像二进制内容。
第二个问题也很简单。
设置您的 PHP 安装以使其在屏幕上显示错误(当然是针对开发服务器!),您将看到对您的问题“为什么这对我不起作用?”的确切答案。
error_reporting(E_ALL);
ini_set('display_errors',1);
通常会有帮助。
(还要确保您的代码不执行任何可能向您隐藏错误消息的 HTTP 重定向)
关于php - 在 file_get_contents 之后在 php 中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8051500/