PHP 文件上传:调整图像大小并在新画布上重新定位

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 文件上传:调整图像大小并在新画布上重新定位相关的知识,希望对你有一定的参考价值。

// NOW UPLOAD THE LOGO
					
					$formname = 'CompanyLogo';				

					if(isset($_FILES[$formname]['name'])){
						$name = stripslashes($_FILES[$formname]['name']);

						if ($name!=""){
							
							$name = str_replace(" ", "_" , $name) ;
							
							//if(move_uploaded_file($_FILES[$formname]['tmp_name'], "../images_cms/{$name}")){
							
							
							$extension = getExtension($name);
							$extension = strtolower($extension);
							if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
								echo ' Unknown Image extension ';
								$errors=1;
							
							}else{
								
								$size=filesize($_FILES[$formname]['tmp_name']);
					 
								//if ($size > MAX_SIZE*1024){
									//echo "You have exceeded the size limit";
									//$errors=1;
								//}
					 
								if($extension=="jpg" || $extension=="jpeg" ){
									$uploadedfile = $_FILES[$formname]['tmp_name'];
									$src = imagecreatefromjpeg($uploadedfile);
								}else if($extension=="png"){
									$uploadedfile = $_FILES[$formname]['tmp_name'];
									$src = imagecreatefrompng($uploadedfile);
								}else{
									$src = imagecreatefromgif($uploadedfile);
								}
											 
								list($width,$height)=getimagesize($uploadedfile);
								
								$canvasW = 230 ;
								$canvasH = 78 ;
						
								$newwidth = $canvasW;
								$newheight = ($height/$width)*$newwidth;
								
								// IF THE PROPOTIONS ARE WRONG
								
								if ($newheight > $canvasH) {
			
									$newheight = $canvasH;
									$newwidth=($width/$height)*$newheight;
								
								}
								
								$offsetY = intval( ($canvasH-$newheight)/2 );
			
								$tmp=imagecreatetruecolor($canvasW,$canvasH);
								
								$white=imagecolorallocate($tmp,255,255,255);
								imagefilledrectangle($tmp,0,0,$canvasW,$canvasH,$white);
								
								// imagecopyresampled ( resource $dst_image , resource $src_image , 
									// int $dst_x , int $dst_y , 
									// int $src_x , int $src_y , 
									// int $dst_w , int $dst_h , int $src_w , int $src_h )
						
								imagecopyresampled($tmp,$src,
									0,$offsetY,
									0,0,
									$newwidth,$newheight,
									$width,$height);
											
								$imgPath = "../images_cms/{$name}" ;
								
								imagejpeg($tmp,$imgPath,100);
								
								imagedestroy($src);
								imagedestroy($tmp);
								


								$sql_cmpy=" UPDATE `cms_companies`  SET `CompanyLogo`='$name' WHERE `companyID`='$companyID' ";
								$result_cmpy=doSQL($sql_cmpy);
							
							} // END if $extention
						} // END if $name
					} // END LOGO

以上是关于PHP 文件上传:调整图像大小并在新画布上重新定位的主要内容,如果未能解决你的问题,请参考以下文章

上传前调整图像大小 - 将画布转换为文件对象

PHP GD - 调整图像框架/画布的大小,但不是实际图像

将画布图像上传到 S3 服务器

画布图像大小调整使文件更大[重复]

通过上传 PHP 在多个图像上以相同的比例调整大小

PHP:图像调整大小和裁剪为纵向