【发布时间】:2023-04-11 01:30:01
【问题描述】:
我正在使用以下代码将图像重新调整为我想要的高度。
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
但是,在我的旧代码中,我将图像保存在同一台服务器上 - 我从 Amazon 学到了一些关于 s3 的知识,并希望将调整大小的图像上传到该服务器。
目前,如果我想将图像上传到 S3,我会执行以下操作
function uploadmedia(){
include('s3upload/image_check.php');
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$tmp = $_FILES['file']['tmp_name'];
$ext = getExtension($name);
if(in_array($ext,$valid_formats))
{
if($size < 1048576)
{
include('s3upload/s3_config.php');
//Rename image name.
$savename = base64_encode($name);
$actual_image_name = $savename.time().".".$ext;
if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
{
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
}
else
$msg = "S3 Upload Fail.";
}
else
$msg = "Image size Max 1 MB";
}
else
{
$msg = "Invalid file, please upload image file.";
}
}
现在回到第一个代码,我通常会通过这种方法保存图像
$image = new SimpleImage();
$image->load($targetFile);
$image->resizeToHeight(80);
$image->save(rtrim($targetPath,'/') . '/' . md5($_FILES['Filedata']['name']) . '-x-h80.' . $ext);
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 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);
}
}
我想知道是否有人知道不将图像保存到旧文件系统,而是将图像重新调整为 80x80 并将其上传到 s3 的方法
谢谢。
这是完整的图像调整脚本
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;
}
}
// Define a destination
$targetFolder = '/uploads'; // Relative to the root
if (!empty($_FILES["Filedata"])) {
$name = $_FILES['Filedata']['name'];
$ext = end(explode(".", $name));
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . md5($_FILES['Filedata']['name']) . '.' . $ext;
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
$link = array();
$link['large'] = "http://www.ipetfindr.com/petuploads/". md5($_FILES['Filedata']['name']) . '.' . $ext;
$link['small'] = "http://www.ipetfindr.com/petuploads/". md5($_FILES['Filedata']['name']) . '-x-h80.' . $ext;
move_uploaded_file($tempFile,$targetFile);
$image = new SimpleImage();
$image->load($targetFile);
$image->resizeToHeight(80);
$image->save(rtrim($targetPath,'/') . '/' . md5($_FILES['Filedata']['name']) . '-x-h80.' . $ext);
echo '1';
}
else
{print "did not work";}
【问题讨论】:
标签: php image file-upload amazon-s3