在PHP中将图像分为三种尺寸
发布时间:2020-12-13 22:46:53 所属栏目:PHP教程 来源:网络整理
导读:我实际上试图将图像分成三种尺寸,然后将它们存储在具有不同名称的服务器文件夹中.我通过移动客户端以 Base64编码格式获取此图像数据.这是我试图将图像转换为三个图像的代码. 这是我接收数据的PHP文件. $uploadPath = 'C:/xampp/htdocs/OSFiles/of-images/ima
|
我实际上试图将图像分成三种尺寸,然后将它们存储在具有不同名称的服务器文件夹中.我通过移动客户端以
Base64编码格式获取此图像数据.这是我试图将图像转换为三个图像的代码.
这是我接收数据的PHP文件. $uploadPath = 'C:/xampp/htdocs/OSFiles/of-images/images/';
if(!is_dir($uploadPath))
mkdir($uploadPath,true) or trigger_error("Can't Create Folder");
$file = tempnam($uploadPath,'image');
$fp = fopen($file,'wb');
fwrite($fp,$binary); //$binary is--> I am reading the image in binary as I am getting the photo data in base64 encoded format through mobile client side. So I decoded it to binary.
fclose($fp);
$result1 = mysql_query("INSERT INTO oc_t_item_resource(fk_i_item_id,s_name,s_extension,s_content_type,s_path)
VALUES('$lastid','$s_name','$extension','$s_content_type','$imagepath')");
$imagelastid = mysql_insert_id();
// Create normal size
$normal_path = $path = $uploadPath . $imagelastid . '.jpg' ;
$size = explode('x','640x480') ;
ImageResizer::fromFile($file)->resizeTo($size[0],$size[1])->saveToFile($path) ;
// Create preview
$path = $uploadPath . $imagelastid . '_preview.jpg' ;
$size = explode('x','480x340') ;
ImageResizer::fromFile($file)->resizeTo($size[0],$size[1])->saveToFile($path) ;
// Create thumbnail
$path = $uploadPath . $imagelastid . '_thumbnail.jpg' ;
$size = explode('x','240x200') ;
ImageResizer::fromFile($file)->resizeTo($size[0],$size[1])->saveToFile($path) ;
这是我在ImageResizer.php文件中的ImageResizer类. <?php
class ImageResizer {
public static function fromFile($imagePath) {
return new ImageResizer($imagePath);
}
private $im;
private function __construct($imagePath) {
if(!file_exists($imagePath)) throw new Exception("$imagePath does not exist!");
if(!is_readable($imagePath)) throw new Exception("$imagePath is not readable!");
if(filesize($imagePath)==0) throw new Exception("$imagePath is corrupt or broken!");
if(osc_use_imagick()) {
$this->im = new Imagick($imagePath);
} else {
$content = file_get_contents($imagePath);
$this->im = imagecreatefromstring($content);
}
return $this;
}
public function __destruct() {
if(osc_use_imagick()) {
$this->im->destroy();
} else {
imagedestroy($this->im);
}
}
public function resizeTo($width,$height) {
if(osc_use_imagick()) {
$bg = new Imagick();
$bg->newImage($width,$height,'white');
$this->im->thumbnailImage($width,true);
$geometry = $this->im->getImageGeometry();
$x = ( $width - $geometry['width'] ) / 2;
$y = ( $height - $geometry['height'] ) / 2;
$bg->compositeImage( $this->im,imagick::COMPOSITE_OVER,$x,$y );
$this->im = $bg;
} else {
$w = imagesx($this->im);
$h = imagesy($this->im);
if(($w/$h)>=($width/$height)) {
//$newW = $width;
$newW = ($w > $width)? $width : $w;
$newH = $h * ($newW / $w);
} else {
//$newH = $height;
$newH = ($h > $height)? $height : $h;
$newW = $w * ($newH / $h);
}
$newIm = imagecreatetruecolor($width,$height);//$newW,$newH);
imagealphablending($newIm,false);
$colorTransparent = imagecolorallocatealpha($newIm,255,127);
imagefill($newIm,$colorTransparent);
imagesavealpha($newIm,true);
imagecopyresampled($newIm,$this->im,(($width-$newW)/2),(($height-$newH)/2),$newW,$newH,$w,$h);
imagedestroy($this->im);
$this->im = $newIm;
}
return $this;
}
public function saveToFile($imagePath) {
if(file_exists($imagePath) && !is_writable($imagePath)) throw new Exception("$imagePath is not writable!");
if(osc_use_imagick()) {
$this->im->setImageFileName($imagePath);
$this->im->writeImage($imagePath);
} else {
imagejpeg($this->im,$imagePath);
}
}
public function show() {
header('Content-Disposition: Attachment;filename=image.jpg');
header('Content-type: image/jpg');
if(osc_use_imagick()) {
} else {
imagepng($this->im);
}
}
}
?>
图像文件夹中根本没有存储图像.我哪里错了? 我们将非常感谢带有更正的代码段. (我是PHP的新人,所以请保持温柔.) 解决方法
我建议在搜索bug时使用error_reporting(-1).然后,您只需按照消息并修复所有错误.
你为mkdir跳过了一个参数.在Windows上忽略$mode,但是为了获得$recursive参数,它应该被传递:mkdir($uploadPath,0777,true). 此外,你应该在某处包含一个函数osc_use_imagick.我没有它所以我不得不将条件从if(osc_use_imagick())更改为if(function_exists(‘osc_use_imagick’)&& osc_use_imagick()). 顺便说一句,在所有调整大小后,您忘记删除临时文件.将unlink($file)添加到脚本的末尾. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
