业务说明:由于ios最大支持2048 * 2048的图片 ,目前系统中有很多超长的图片 因此需要进行裁剪,而且要修改数据库中一个json格式的字段,需要在长图的位置删除原对象紧接着插入分割后的图片地址
//图片分割 ios 图片不要超过 2048 private function excisionImage(Good $good, $storagePath) { $imgListJson = json_decode($good->getAttributes()['image_list'], true);//$image->image_list; $willToChange = []; foreach ($imgListJson as $key => $item) { if($imgUrl = $item['url']) { //构造路径 $filePath = str_replace('storage', $storagePath, $imgUrl); //获取图片信息用于判断 if(file_exists($filePath)) { $arrPath = pathinfo($filePath); $path = $arrPath['dirname']; $fileName = $arrPath['filename']; $extName = $arrPath['extension']; list($width, $height, $type, $attr) = getimagesize($filePath); if($height > self::IMAGE_LENGTH) { if($type == IMAGETYPE_JPEG) { $funExt = 'jpeg';//imagecreatefromjpeg($filePath) } elseif($type == IMAGETYPE_PNG) { $funExt = 'png'; } elseif($type == IMAGETYPE_GIF) { $funExt = 'gif'; } $funCreate = "imagecreatefrom{$funExt}"; $img = $funCreate($filePath); $maxW = $width; $maxH = self::IMAGE_LENGTH; $funImg = "image{$funExt}"; $arrImgFg = []; for ($i = 0; $i < ceil($height / $maxH); $i++) { $newfile = "{$path}/{$fileName}_{$i}." . $extName; if ($i == (ceil($height / $maxH) - 1)) { $maxHx = $height - $maxH * $i; $iOut = @imagecreatetruecolor($maxW, $maxHx); } else { $iOut = @imagecreatetruecolor($maxW, $maxH); } imagecopy($iOut, $img, 0, 0, 0, $i * $maxH, $maxW, $maxH); $funImg($iOut, $newfile); $newPath = str_replace($storagePath, 'storage', $newfile); $arrImgFg[] = ['url' => $newPath, 'carousel' => 0]; } $willToChange[$key] = $arrImgFg; imagedestroy($img); } } } } $offset = 0; $arrKeyOffset = []; foreach ($willToChange as $key => $imgList) { $arrKeyOffset[] = $key + $offset; array_splice($imgListJson, $key + 1 + $offset, 0, $imgList); // 插入到此位置 且删除0个 $offset += count($imgList); } foreach ($arrKeyOffset as $key) { unset($imgListJson[$key]); } return $imgListJson; }