$item_pic = "img/item.jpg"; list($width, $height) = getimagesize($item_pic); $item_pic = imagecreatefromjpeg($item_pic); $avg_width = ceil($width / 3); $avg_height = ceil($height / 3); // 创建画布 for ($i = 1; $i < 10; $i++) { $image = imagecreatetruecolor($avg_width, $avg_height); list($source_width, $source_height) = get_source_width_height($avg_width, $avg_height, $i); imagecopyresampled($image, $item_pic, 0, 0, $source_width, $source_height, $avg_height, $avg_height, $avg_height, $avg_height); imagejpeg($image, 'img/'.$i.'.jpg'); imagedestroy($image); } function get_source_width_height ($avg_width, $avg_height, $i) { switch ($i) { case 1: $return_width = 0; $return_height = 0; break; case 2: $return_width = $avg_width; $return_height = 0; break; case 3: $return_width = $avg_width * 2; $return_height = 0; break; case 4: $return_width = 0; $return_height = $avg_height; break; case 5: $return_width = $avg_width ; $return_height = $avg_height; break; case 6: $return_width = $avg_width * 2; $return_height = $avg_height; break; case 7: $return_width = 0; $return_height = $avg_height * 2; break; case 8: $return_width = $avg_width; $return_height = $avg_height * 2; break; case 9: $return_width = $avg_width * 2; $return_height = $avg_height * 2; break; default; } return [$return_width, $return_height]; }
效果