0

我将此图像调整大小类用于公司的徽标大小调整过程:

include 'resize-class.php';

function checkImageReq($image){

    $size = (int) formatBytes(filesize($image)); 

    list($width, $height) = getimagesize($image);

    if($size > 400){
        return FALSE;
    }

    if($width > 300 || $width < 50){
        return FALSE;
    }

    if($height > 300 || $height < 50){
        return FALSE;
    }

    return TRUE;

}


// allows file types
$allowed = array('jpg','png');

// get file type
$extension = pathinfo($_FILES['company_logo']['name'], PATHINFO_EXTENSION);

// I added below methods to create auto resize
$resizeObj = new resize($_FILES['company_logo']['tmp_name']);
$resizeObj -> resizeImage(250, 250, 'auto');
$resizeObj -> saveImage('img.jpg', 1000);


// is there new logo that needs updating
if($_FILES['company_logo']['error'] === 0 && in_array($extension, $allowed)){

    $uploaddir = 'images/logo/';
    $filename = $mainUser->getUserId().'_'.time().'_'.$_FILES['company_logo']['name'];
    $uploadfile = $uploaddir . basename($filename);

    if(checkImageReq($_FILES['company_logo']['tmp_name']) && move_uploaded_file($_FILES['company_logo']['tmp_name'], $uploadfile)){

        // delete old logo
        if(file_exists($uploaddir.$mainUser->getCompanyLogo())){
            unlink($uploaddir.$mainUser->getCompanyLogo());
        }

        $mainUser->setCompanyLogo($filename);

    }else{

        $image_error = 'Error uploading logo.';

        goto end;

    }

当我运行这个脚本时,我得到了宽度和高度为 250 像素的新EMPTY$resizeObj = new resize($_FILES['company_logo']['tmp_name']);图像,我怀疑它永远不会通过real图像来执行自动调整大小。

有人可以建议如何将此类与我现有的代码结合起来吗?

谢谢。

4

1 回答 1

0

1.首先你需要设置这个 php 设置:

ini_set('error_reporting', E_ALL | E_STRICT); //to output all errors on screen.

2.改变:

$resizeObj -> saveImage('img.jpg', 1000); // within accepted ranges 0-100

3.您必须在初始化调整大小对象时在构造函数中将路径设置为本地存储文件的路径,而不是临时文件。

ini_set('error_reporting', E_ALL | E_STRICT);
include 'resize-class.php';

function checkImageReq($image){

    $size = (int) formatbytes(filesize($image)); 

    list($width, $height) = getimagesize($image);

    if($size > 400){
        return FALSE;
    }

    if($width > 300 || $width < 50){
        return FALSE;
    }

    if($height > 300 || $height < 50){
        return FALSE;
    }

    return TRUE;

}


// allows file types
$allowed = array('jpg','png');

// get file type
$extension = pathinfo($_FILES['company_logo']['name'], PATHINFO_EXTENSION);


// is there new logo that needs updating
if($_FILES['company_logo']['error'] === 0 && in_array($extension, $allowed)){

    $uploaddir = 'images/logo/';
    $filename = $mainUser->getUserId().time().'_'.$_FILES['company_logo']['name'];
    $uploadfile = $uploaddir . basename($filename);

    if(checkImageReq($_FILES['company_logo']['tmp_name']) && move_uploaded_file($_FILES['company_logo']['tmp_name'], $uploadfile)){
    
        // I added below methods to create auto resize
        $resizeObj = new resize($uploadfile);
        $resizeObj -> resizeImage(250, 250, 'auto');
        $resizeObj -> saveImage($uploaddir. basename("img.png"), 100);
        
        // delete old logo
        if(file_exists($uploaddir.$mainUser->getCompanyLogo())){
            unlink($uploaddir.$mainUser->getCompanyLogo());
        }

        $mainUser->setCompanyLogo($filename);
        
    }else{
        $image_error = 'Error uploading logo.';
        goto end;
    }
}
于 2014-04-21T16:41:58.087 回答