PHP는 워터마크를 추가하고 Thumbnails_php 기술을 생성할 수 있는 이미지 처리 도구를 구현합니다.

jacklove
풀어 주다: 2023-04-02 07:44:01
원래의
1498명이 탐색했습니다.

이 글에서는 주로 워터마크를 추가하고 썸네일을 생성할 수 있는 이미지 처리 도구를 구현하는 PHP를 소개합니다. 여기에는 이미지 표시, 저장, 압축, 워터마크 등의 PHP 관련 조작 기술이 포함됩니다. 필요한 친구는 참고할 수 있습니다.

이 글에서는 설명합니다. 예제 PHP를 사용하여 워터마크를 추가하고 썸네일을 생성할 수 있는 이미지 처리 도구 클래스를 구현합니다. 참조를 위해 모든 사람과 공유하십시오. 세부 사항은 다음과 같습니다.

그런 다음 ImageTool 개체를 변환합니다.

<?php
class ImageTool
{
  private $imagePath;//图片路径
  private $outputDir;//输出文件夹
  private $memoryImg;//内存图像
  public function __construct($imagePath, $outputDir = null)
  {
    $this->imagePath = $imagePath;
    $this->outputDir = $outputDir;
    $this->memoryImg = null;
  }
  /**
   * 显示内存中的图片
   * @param $image
   */
  public function showImage()
  {
    if ($this->memoryImg != null) {
      $info = getimagesize($this->imagePath);
      $type = image_type_to_extension($info[2], false);
      header(&#39;Content-type:&#39; . $info[&#39;mime&#39;]);
      $funs = "image{$type}";
      $funs($this->memoryImg);
      imagedestroy($this->memoryImg);
      $this->memoryImg = null;
    }
  }
  /**将图片以文件形式保存
   * @param $image
   */
  private function saveImage($image)
  {
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $funs = "image{$type}";
    if (empty($this->outputDir)) {
      $funs($image, md5($this->imagePath) . &#39;.&#39; . $type);
    } else {
      $funs($image, $this->outputDir . md5($this->imagePath) . &#39;.&#39; . $type);
    }
  }
  /**
   * 压缩图片
   * @param $width 压缩后宽度
   * @param $height 压缩后高度
   * @param bool $output 是否输出文件
   * @return resource
   */
  public function compressImage($width, $height, $output = false)
  {
    $image = null;
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $fun = "imagecreatefrom{$type}";
    $image = $fun($this->imagePath);
    $thumbnail = imagecreatetruecolor($width, $height);
    imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
    imagedestroy($image);
    if ($output) {
      $this->saveImage($thumbnail);
    }
    $this->memoryImg = $thumbnail;
    return $this;
  }
  /**
   * 为图像添加文字标记
   *
   * @param $content 文本内容
   * @param $size 字体大小
   * @param $font 字体样式
   * @param bool $output 是否输出文件
   * @return $this
   */
  public function addTextmark($content, $size, $font, $output = false)
  {
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $fun = "imagecreatefrom{$type}";
    $image = $fun($this->imagePath);
    $color = imagecolorallocatealpha($image, 0, 0, 0, 80);
    $posX = imagesx($image) - strlen($content) * $size / 2;
    $posY = imagesy($image) - $size / 1.5;
    imagettftext($image, $size, 0, $posX, $posY, $color, $font, $content);
    if ($output) {
      $this->saveImage($image);
    }
    $this->memoryImg = $image;
    return $this;
  }
  /**
   * 为图片添加水印
   *
   * @param $watermark 水印图片路径
   * @param $alpha 水印透明度(0-100)
   * @param bool $output 是否输出文件
   * @return $this
   */
  public function addWatermark($watermark, $alpha, $output = false)
  {
    $image_info = getimagesize($this->imagePath);
    $image_type = image_type_to_extension($image_info[2], false);
    $image_fun = "imagecreatefrom{$image_type}";
    $image = $image_fun($this->imagePath);
    $mark_info = getimagesize($watermark);
    $mark_type = image_type_to_extension($mark_info[2], false);
    $mark_fun = "imagecreatefrom{$mark_type}";
    $mark = $mark_fun($watermark);
    $posX = imagesx($image) - imagesx($mark);
    $posY = imagesy($image) - imagesy($mark);
    imagecopymerge($image, $mark, $posX, $posY, 0, 0, $mark_info[0], $mark_info[1], $alpha);
    if ($output) {
      $this->saveImage($image);
    }
    $this->memoryImg = $image;
    return $this;
  }
}
로그인 후 복사

1. 압축된 이미지 생성

require_once &#39;ImageTool.class.php&#39;;
로그인 후 복사

2. 텍스트 워터마크 추가

$imageTool = new ImageTool(&#39;img/oppman.jpeg&#39;, &#39;out/&#39;);//图片路径、输出文件夹
로그인 후 복사

3. 사진 추가 워터마크

$imageTool->compressImage(350, 250, true);//压缩宽度、压缩高度、是否保存
$imageTool->showImage();
로그인 후 복사

는 임시 이미지 출력으로만 사용됩니다.

$imageTool->addTextmark(&#39;一拳超人&#39;, 50, &#39;res/micro.ttf&#39;, true);//内容、尺寸、字体、是否保存
$imageTool->showImage();
로그인 후 복사

PS: 다음은 귀하에게 권장되는 좀 더 실용적인 이미지 처리 도구입니다. 참조:

온라인 이미지 자르기/생성 도구:

http://tools.jb51.net/aideddesign/imgcut

온라인 이미지 변환 BASE64 도구:

http://tools. jb51.net/transcoding /img2base64

ICO 아이콘 온라인 생성 도구: http://tools.jb51.net/aideddesign/ico_img

온라인 이메일 아이콘 생성 도구:
http:// tools.jb51.net/email/mailillogo

온라인 이미지 형식 변환(jpg/bmp/gif/png) 도구:
http://tools.jb51.net/aideddesign/picext

기사 관심이 있을 수 있습니다:
PHP가 지그재그 순서로 이진 트리를 인쇄하는 방법에 대한 설명


PHP가 이진 트리 이미지를 얻는 방법에 대한 설명

PHP가 마지막 노드에서 K번째 노드를 얻는 방법에 대한 설명 연결리스트

위 내용은 PHP는 워터마크를 추가하고 Thumbnails_php 기술을 생성할 수 있는 이미지 처리 도구를 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!