4

我实际上是在尝试将图像制作成三种尺寸,然后将它们以不同的名称存储在服务器文件夹中。我通过移动客户端以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新手,所以请温柔。)

4

1 回答 1

1

我建议error_reporting( -1 )在搜索错误时使用。然后,您只需按照消息并修复所有错误。

您跳过了mkdir的一个参数。$mode 在 Windows 上被忽略,但无论如何都应该传递它以获取 $recursive 参数:mkdir( $uploadPath, 0777, true )

此外,您应该在osc_use_imagick某处包含一个功能。我没有它,所以我不得不将条件从 更改if( osc_use_imagick() )if( function_exists( 'osc_use_imagick' ) && osc_use_imagick() )

顺便说一句,您忘记在所有调整大小后删除您的临时文件。添加unlink( $file )到脚本的末尾。

于 2013-01-01T21:04:01.387 回答