我实际上是在尝试将图像制作成三种尺寸,然后将它们以不同的名称存储在服务器文件夹中。我通过移动客户端以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, $height, 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, 255, 255, 127);
imagefill($newIm, 0, 0, $colorTransparent);
imagesavealpha($newIm, true);
imagecopyresampled($newIm, $this->im, (($width-$newW)/2), (($height-$newH)/2), 0, 0, $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新手,所以请温柔。)