0

我有这段代码,我之前稍微修改了一下。我的问题是这会为我上传的图像创建一个新的缩略图,但它会添加一个我不需要的 .PNG、.JPEG 等扩展名!现在,我的问题是,如果我尝试从函数中删除扩展,它不会让我创建新图像......

我的意思是,如果我从 createFile 函数中删除 : '.'.$this->ext ,它将不再创建文件......

这是代码......它似乎有点长,但它不是......

if (!empty($_FILES)) {


function setFile($src = null) {
    $this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));

if(is_file($src) && ($this->ext == "JPG" OR $this->ext == "JPEG")) {
        $this->img_r = ImageCreateFromJPEG($src);
    } elseif(is_file($src) && $this->ext == "PNG") {
        $this->img_r = ImageCreateFromPNG($src);
    } elseif(is_file($src) && $this->ext == "GIF") {
        $this->img_r = ImageCreateFromGIF($src);
    }

$this->img_w = imagesx($this->img_r);
    $this->img_h = imagesy($this->img_r);
}

function resize($largestSide = 100) {
    $width = imagesx($this->img_r);
    $height = imagesy($this->img_r);
    $newWidth = 0;
    $newHeight = 0;

    if ($width > $height) {
        $newWidth = $largestSide;
        $newHeight = $height * ($newWidth / $width);
    } else {
        $newHeight = $largestSide;
        $newWidth = $width * ($newHeight / $height);
    }

    $this->dst_r = ImageCreateTrueColor($newWidth, $newHeight);
    imagecopyresampled($this->dst_r, $this->img_r, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $this->img_r = $this->dst_r;
    $this->img_h = $newHeight;
    $this->img_w = $newWidth;
}

function createFile($output_filename = null) {
    if ($this->ext == "JPG" OR $this->ext == "JPEG") {
        imageJPEG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext, $this->quality);
    } elseif($this->ext == "PNG") {
        imagePNG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
    } elseif($this->ext == "GIF") {
        imageGIF($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
    }

    $this->output = $this->uploaddir.$output_filename.'.'.$this->ext;
}

function setUploadDir($dirname) {
$this->uploaddir = $dirname;
}

function flush() {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

imagedestroy($this->dst_r);
unlink($targetFile);
imagedestroy($this->img_r);

}

}

$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

move_uploaded_file ($tempFile, $targetFile);

$image = new Image();
$image->setFile($targetFile);
$image->setUploadDir($targetPath);
$image->resize(800);
$image->createFile($_FILES['Filedata']['name']);
$image->flush();
}
4

1 回答 1

0

在格式化您的代码并在末尾删除额外的紧卷曲后(同时将您的函数定义移到您的if语句之外),您的代码应该如下所示:

<?php

function setFile($src = null) {
    $this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));
    if (is_file($src) && ($this->ext == "JPG" OR $this->ext == "JPEG")) {
        $this->img_r = ImageCreateFromJPEG($src);
    } elseif (is_file($src) && $this->ext == "PNG") {
        $this->img_r = ImageCreateFromPNG($src);
    } elseif (is_file($src) && $this->ext == "GIF") {
        $this->img_r = ImageCreateFromGIF($src);
    }
    $this->img_w = imagesx($this->img_r);
    $this->img_h = imagesy($this->img_r);
}

function resize($largestSide = 100) {
    $width     = imagesx($this->img_r);
    $height    = imagesy($this->img_r);
    $newWidth  = 0;
    $newHeight = 0;

    if ($width > $height) {
        $newWidth  = $largestSide;
        $newHeight = $height * ($newWidth / $width);
    } else {
        $newHeight = $largestSide;
        $newWidth  = $width * ($newHeight / $height);
    }

    $this->dst_r = ImageCreateTrueColor($newWidth, $newHeight);
    imagecopyresampled($this->dst_r, $this->img_r, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $this->img_r = $this->dst_r;
    $this->img_h = $newHeight;
    $this->img_w = $newWidth;
}

function createFile($output_filename = null) {
    if ($this->ext == "JPG" OR $this->ext == "JPEG") {
        imageJPEG($this->dst_r, $this->uploaddir . $output_filename . '.' . $this->ext, $this->quality);
    } elseif ($this->ext == "PNG") {
        imagePNG($this->dst_r, $this->uploaddir . $output_filename . '.' . $this->ext);
    } elseif ($this->ext == "GIF") {
        imageGIF($this->dst_r, $this->uploaddir . $output_filename . '.' . $this->ext);
    }
    $this->output = $this->uploaddir . $output_filename . '.' . $this->ext;
}

function setUploadDir($dirname) {
    $this->uploaddir = $dirname;
}

function flush() {
    $tempFile   = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile = str_replace('//', '/', $targetPath) . $_FILES['Filedata']['name'];

    imagedestroy($this->dst_r);
    unlink($targetFile);
    imagedestroy($this->img_r);
}

if (!empty($_FILES)) { 
    $tempFile   = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile = str_replace('//', '/', $targetPath) . $_FILES['Filedata']['name'];

    move_uploaded_file($tempFile, $targetFile);

    $image = new Image();
    $image->setFile($targetFile);
    $image->setUploadDir($targetPath);
    $image->resize(800);
    $image->createFile($_FILES['Filedata']['name']);
    $image->flush();
}

这仍然不起作用,因为您正在调用这些方法,就好像它们Image是未在任何地方定义的对象的成员函数一样。查看PHP 手册中的对象和类

于 2012-08-20T14:38:38.493 回答