PHP图片上传实现压缩并控制宽高接口函数

接口调用:

public function execute() {
	$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
	$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
	$typeJson = ResultFactory::TYPE_JSON;
	$resultJson = $this->resultFactory->create($typeJson);

	if (isset($_FILES['upimage'])) {
		//Get extention name
		$arrExt = explode('.', $_FILES["upimage"]["name"]);
		$extName = $arrExt[1];

		//Rename file
		$newName = time().'.'.$extName;
		$newImage = $this->mk_dir().$newName;

		//Upload file & Return full path
		$maxWidth="1024";
		$maxHeight="1366"; 
		$imgSource = $_FILES["upimage"]["tmp_name"];
		$fileType = $_FILES["upimage"]["type"];
		if ($this->thumbImage($imgSource, $maxWidth, $maxHeight, $newImage, $fileType)) {
			//Return full path
			$urlTypeMedia = \Magento\Framework\UrlInterface::URL_TYPE_MEDIA;
			$fullUrl = $storeManager->getStore()->getBaseUrl($urlTypeMedia).'Plazathemes/blog/club/'.$newName;

			$success = true;
		} else {
			$success = false;
		}
	}

	//Return data
	if ($success) {
		$res['status'] = 200;
		$res['path'] = $fullUrl;
		$res['flagnum'] = $_POST['flagnum'];
	} else {
		$res['status'] = 110;
		$res['path'] = '';
	}

	$resultJson->setData($res);
	return $resultJson;
}

接口函数如下:


/**
* Compress images
*/
public function thumbImage($im, $maxwidth, $maxheight, $name, $filetype) {
	switch ($filetype) { 
		case 'image/pjpeg':
		case 'image/jpeg':
			$im = imagecreatefromjpeg($im);
			break;
		case 'image/gif':
			$im = imagecreatefromgif($im);
			break; 
			case 'image/png': 
			$im = imagecreatefrompng($im);
			break;
		case 'image/wbmp': 
			$im = imagecreatefromwbmp($im);
			break;
	}

	$resizewidth_tag = $resizeheight_tag = false;
	$pic_width = imagesx($im);
	$pic_height = imagesy($im);

	if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
		$resizewidth_tag = $resizeheight_tag = false;

		if ($maxwidth && $pic_width > $maxwidth) {
			$widthratio = $maxwidth / $pic_width;
			$resizewidth_tag = true;
		}

		if ($maxheight && $pic_height > $maxheight) {
			$heightratio = $maxheight / $pic_height;
			$resizeheight_tag = true;
		}

		if ($resizewidth_tag && $resizeheight_tag) {
			if ($widthratio < $heightratio) {
				$ratio = $widthratio;
			} else {
				$ratio = $heightratio;
			}
		}

		if ($resizewidth_tag && !$resizeheight_tag) {
			$ratio = $widthratio;
		}

		if ($resizeheight_tag && !$resizewidth_tag) {
			$ratio = $heightratio;
		}

		$newwidth = $pic_width * $ratio;
		$newheight = $pic_height * $ratio;

		if (function_exists("imagecopyresampled")) {
			$newim = imagecreatetruecolor($newwidth, $newheight);
			imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
		} else {
			$newim = imagecreate($newwidth, $newheight);
			imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
		}

		switch ($filetype) { 
			case 'image/pjpeg' : 
			case 'image/jpeg' : 
				$result = imagejpeg($newim,$name); 
				break; 
			case 'image/gif' : 
				$result = imagegif($newim,$name); 
				break; 
			case 'image/png' : 
				$result = imagepng($newim,$name);
				break;
			case 'image/wbmp' : 
				$result = imagewbmp($newim,$name);
				break;
		}
		imagedestroy($newim);
	} else {
		switch ($filetype) { 
			case 'image/pjpeg' : 
			case 'image/jpeg' : 
				$result = imagejpeg($im,$name); 
				break; 
			case 'image/gif' : 
				$result = imagegif($im,$name); 
				break; 
			case 'image/png' : 
				$result = imagepng($im,$name);
				break;
			case 'image/wbmp' : 
				$result = imagewbmp($im,$name);
				break;
		}
	}
	return $result;
} 

郑重声明:

1 本资源来源于互联网,资源的版权归资源原作者所持有,受《中华人民共和国著作权法》等相关法律保护。

2 由于无法和原作者取得联系,所以上传的部分资源无法先通过原作者的同意就分享给大家了,如本资源侵犯了您(原作者)的权益,请联系我们(微信号 xiaohaimei1989),我们会立马删除您的资源,并向您表达诚挚的歉意!

3 本站是一个公益型网站,分享资源的目的在于传播知识,分享知识,收取一点点打赏的辛苦费是用于网站的日常运营开支,并非用于商业用途。

4 本站资源只提供学习和参考研究使用,使用过后请在第一时间内删除。本站不承担资源被单位或个人商用带来的法律责任。

发表评论