我将此image resize class用于公司的徽标调整大小过程:
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;
}
当我运行此脚本时,我得到了我的新 EMPTY 图片,其宽度和高度为250px,我怀疑$resizeObj = new resize($_FILES['company_logo']['tmp_name']);
从未通过real
图片执行自动调整大小。
有人可以建议如何将这个课程与我现有的代码结合起来吗?
感谢。
答案 0 :(得分:0)
1.首先你需要设置这个php设置:
ini_set('error_reporting', E_ALL | E_STRICT); //to output all errors on screen.
2.Change:
$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;
}
}