中文天堂在线播放|777超碰|亚洲男人天堂手机版|露水红颜电影未删减完整版,简单任务国语高清在线观看完整版,成人xxxxx,日本aa在线

php生成縮略圖的兩種方法

時間:2025-12-15 02:48:02 php語言

php生成縮略圖的兩種方法

  在php中如果要生成縮略圖有什么辦法呢?以下是小編整理的兩種方法,希望對大家有用,更多消息請關注應屆畢業(yè)生網(wǎng)。
  核心代碼如下
  public function getImage(){
  /pic/div>
  $basePath = base_path('public/');
  $md5 = 'b8c3bf0b10a81c5b6a98c527868a0000';
  $sub1 = substr($md5, 0, 1);
  $sub2 = substr($md5, 1, 2);
  $subPath = sprintf("image/%s/%s/%s%s.jpg", $sub1, $sub2, $md5, '300x300');
  $sourcePath = sprintf("image/%s/%s/%s.jpg", $sub1, $sub2, $md5);
  /pic/div>
  /pic/div>
  $image = $this->getCrops($basePath.$sourcePath,$basePath.$subPath, 300, 300);
  }
  /pic/div>
  function getCropper($source_path,$NewImagePath, $target_width, $target_height)
  {
  $source_info   = getimagesize($source_path);
  $source_width  = $source_info[0];
  $source_height = $source_info[1];
  $source_mime   = $source_info['mime'];
  $source_ratio  = $source_height / $source_width;
  $target_ratio  = $target_height / $target_width;
  /pic/div>
  if ($source_ratio > $target_ratio)
  {
  $cropped_width  = $source_width;
  $cropped_height = $source_width * $target_ratio;
  $source_x = 0;
  $source_y = ($source_height - $cropped_height) / 2;
  }
  /pic/div>
  elseif ($source_ratio < $target_ratio)
  {
  $cropped_width  = $source_height / $target_ratio;
  $cropped_height = $source_height;
  $source_x = ($source_width - $cropped_width) / 2;
  $source_y = 0;
  }
  /pic/div>
  else
  {
  $cropped_width  = $source_width;
  $cropped_height = $source_height;
  $source_x = 0;
  $source_y = 0;
  }
  switch ($source_mime)
  {
  case 'image/gif':
  $source_image = imagecreatefromgif($source_path);
  break;
  case 'image/jpeg':
  $source_image = imagecreatefromjpeg($source_path);
  break;
  case 'image/png':
  $source_image = imagecreatefrompng($source_path);
  break;
  default:
  return false;
  break;
  }
  $target_image  = imagecreatetruecolor($target_width, $target_height);
  $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
  /pic/div>
  imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
  /pic/div>
  imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
  header('Content-Type: image/jpeg');
  imagejpeg($target_image,$NewImagePath,100);
  imagedestroy($source_image);
  imagedestroy($target_image);
  imagedestroy($cropped_image);
  }
  /pic/div>
  function getCrops($src_path,$NewImagePath,$width,$height){
  /pic/div>
  $src_image = imagecreatefromstring(file_get_contents($src_path));
  $source_info   = getimagesize($src_path);
  $source_mime   = $source_info['mime'];
  $src_width = imagesx($src_image);
  $src_height = imagesy($src_image);
  switch ($source_mime)
  {
  case 'image/gif':
  $src_image = imagecreatefromgif($src_path);
  break;
  case 'image/jpeg':
  $src_image = imagecreatefromjpeg($src_path);
  break;
  case 'image/png':
  $src_image = imagecreatefrompng($src_path);
  break;
  default:
  return false;
  break;
  }
  /pic/div>
  /pic/div>
  /pic/div>
  if ($src_width / $src_height >= $width / $height) {
  $tmp_image_width = $width;
  $tmp_image_height = round($tmp_image_width * $src_height / $src_width);
  } else {
  $tmp_image_height = $height;
  $tmp_image_width = round($tmp_image_height * $src_width / $src_height);
  }
  $tmpImage = imagecreatetruecolor($tmp_image_width, $tmp_image_height);
  imagecopyresampled($tmpImage, $src_image, 0, 0, 0, 0, $tmp_image_width, $tmp_image_height, $src_width, $src_height);
  /pic/div>
  $final_image = imagecreatetruecolor($width, $height);
  $color = imagecolorallocate($final_image, 255, 255, 255);
  imagefill($final_image, 0, 0, $color);
  $x = round(($width - $tmp_image_width) / 2);
  $y = round(($height - $tmp_image_height) / 2);
  imagecopy($final_image, $tmpImage, $x, $y, 0, 0, $tmp_image_width, $tmp_image_height);
  /pic/div>
  header('Content-Type: image/jpeg');
  imagejpeg($final_image,$NewImagePath,100);
  imagedestroy($src_image);
  imagedestroy($final_image);
  }

【php生成縮略圖的兩種方法】相關文章:

php生成縮略圖的兩種方法(代碼)11-03

PHP生成縮略圖的方法03-17

PHP生成縮略圖的類的方法10-14

php生成圖片縮略圖的方法11-16

php上傳圖片生成縮略圖02-17

php生成高清縮略圖實例02-05

php生成圖片縮略圖功能示例12-13

PHP生成圖片縮略圖類示例代碼03-08

PHP生成樹的方法介紹10-07