在PHP中将图像分为三种尺寸

时间:2012-12-24 13:11:04

标签: php image storage

我实际上是想将图像分成三种尺寸,然后将它们存储在具有不同名称的服务器文件夹中。我通过移动客户端以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 类,位于ImageResizer.php文件中。

<?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的新手,所以请保持温和。)

1 个答案:

答案 0 :(得分:1)

我建议在搜索错误时使用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 )添加到脚本的末尾。