如果图片超过php.ini的文件大小的话就提示图片过大 如果图片超过2m就等比例压缩图片0.9 ------------------------------------------------------------------------ 如果 upload_max_filesize==10M 就会报如下的错 <br /> <b>Fatal error</b>: Allowed memory size of 239075328 bytes exhausted (tried to allocate 32768 bytes) in <b>F:\SVN\b2c_shop\app\include\lib_image.class.php</b> on line <b>442</b><br /> 需要将php.ini的memory_limit = 516M ------------------------------------------------------------------ 如果 upload_max_filesize==5M 就可以正常上传
/** * 上传文件到CDN,必须在upload目录 * rsync_upload.pass 文件owner必须为www-data,权限必须为600 * @param $upload_relative_path //相对路径 * @return bool */ function upload_cdn_file($upload_relative_path) { // $GLOBALS['_CFG']['CDN_UPLOAD_PATH'] = root@10.10.87.207::upload ; $cdn_upload_path = $GLOBALS['_CFG']['CDN_UPLOAD_PATH']; if (!$cdn_upload_path) return true; if (!file_exists(ROOTDIR . $upload_relative_path)) return false; if (strpos($upload_relative_path, "upload/") !== 0) return false; $relative_file = substr($upload_relative_path, 7); $rsync_cmd = "cd " . ROOTDIR . "upload/ && /usr/bin/rsync {$relative_file} -zvrtopgR --password-file=" . ROOTDIR . "install/rsync_upload.pass {$cdn_upload_path}"; @shell_exec($rsync_cmd); return true; }
public function upload() { $img = new lib_image('tmp_upload'); $elname = $_POST['elname']; Log::LOG('image', print_r($_FILES, true)); Log::LOG('image', print_r($_POST, true)); //UPLOAD_ERR_INI_SIZE 值:1; 上传的文件超过了 php.ini 中 upload_max_filesize选项限制的值 if ($_FILES[$elname]['error'][0]==1) { make_json_error('图片过大,请重新选择图片!'); }else{ if (lib_image::file_type($_FILES[$elname]['type'][0]) == '') { make_json_error('upload_image_format_novalid'); } $path = $img->upload_file($_FILES[$elname]['tmp_name'][0], $_FILES[$elname]['type'][0]); if ($path == '') { make_json_error('upload_image_failed'); } upload_cdn_file($path); if(intval($_FILES[$elname]['size'][0])>1048576){ lib_image::image_png_size_add($path,$path); } log::log('image_png_size_add','image_path:'.print_r($path,true)); $url = lib_util::get_image_url($path); $dim = lib_image::img_wh(ROOTDIR . $path); $rst = array('url' => $url, 'path' => $path, 'width' => $dim['width'], 'height' => $dim['height']); make_json_response($rst, 0); } }
/** * desription 压缩图片 * @param sting $imgsrc 图片路径 * @param string $imgdst 压缩后保存路径 */ static function image_png_size_add($imgsrc,$imgdst){ list($width,$height,$type)=getimagesize($imgsrc); $new_width = $width*0.9; $new_height =$height*0.9; switch($type){ case 1: $giftype=check_gifcartoon($imgsrc); if($giftype){ header('Content-Type:image/gif'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromgif($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); } break; case 2: header('Content-Type:image/jpeg'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); break; case 3: header('Content-Type:image/png'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); break; } } /** * desription 判断是否gif动画 * @param sting $image_file图片路径 * @return boolean t 是 f 否 */ static function check_gifcartoon($image_file){ $fp = fopen($image_file,'rb'); $image_head = fread($fp,1024); fclose($fp); return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true; }
上传等比例压缩图片
最新推荐文章于 2023-07-06 16:59:52 发布