接口调用:
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; }