


Generate thumbnails in php (convert text into graphics)_PHP tutorial
php generates thumbnails (convert text into graphics) This is a classic PHP thumbnail generation function developed by the Office. The file is flexible and practical. It can generate pictures of any style and convert text into graphics.
php tutorial to generate thumbnails (convert text into graphics)
This is a classic PHP thumbnail generation function developed by Banwai. The file is flexible and practical. It can generate pictures of any style and convert text into graphics.
copyright : smart-info limited. all right reserved.
author : jacky cheung
version : 1.1
create date : 24 september 2008
last modify : 15 march 2009
*/class gd
{
var $font_face = "";
var $text = "";
var $size = 12;
var $color = "#000000";
var $angle = 0;var $width = 0;
var $height = 0;
var $line_height = 0;
var $type = "png";
var $chmod = 0777;
var $bg_color = "#ffffff";
var $quality = 95;
var $antialias = true;
var $x = 0;
var $y = 0;
/*___| convert text to image |___*/
public function text2image ( $font_face="", $text="", $attributes=false, $width=0, $all=false )
{
$this->font_face = $font_face;
$this->text = $text;
$this->width = $width;
$this->size = 12;
$this->color = "#000000";
$this->angle = 0;
$this->line_height = 0;
$this->setprop ( $attributes );
if ( $this->width == 0 )
{
return $this->convert_text2image ( $this->text, $this->check_text_width( $this->text ) );
} else {
// word wrap
if ( $all === false )
{
$text = split(" ", $this->text);
$text = $this->word_wrap($this->text, $this->width, "
");
$text = split("
", $text );
} else if ( $all === true ) {
$temp = array();
for ( $i=0; $itext); $i++ )
{
array_push ( $temp, mb_substr($this->text, $i, 1, "utf-8") );
}
$text = array();
$count_width = 0;
$i = 0;
foreach ( $temp as $k => $t )
{
$prop = $this->check_text_width($t);
if ( $count_width + floatval($prop["width"]) width )
{
$text[$i] = $text[$i] . $t;
$count_width += floatval($prop["width"]);
} else {
$count_width = 0;
$i++;
$text[$i] = "";
}
}
}
$img = array();
foreach ( $text as $k => $t )
{
$img[$k] = $this->convert_text2image ( $t, $this->check_text_width( $t ) );
}
$w = 0;
$h = 0;
foreach ( $img as $k => $v )
{
$w = (imagesx($img[$k]) > $w) ? imagesx($img[$k]) : $w;if ($this->line_height == 0 ) $h += imagesy($img[$k]);
else $h += ($k line_height : imagesy($img[$k]);
}
$base_img = $this->createtransparent($w, $h);
$locy = 0;
foreach ( $img as $k => $v )
{
if ($k > 0)
{
$locy = ($this->line_height == 0) ? $locy + imagesy($img[$k]) : $locy + $this->line_height;
}
$base_img = $this->attachgdimage ( $img[$k], $base_img, array ("x"=>0, "y"=>$locy) );
}
return $base_img;
}
}
private function word_wrap( $str, $width, $break )
{
$formatted = '';
$position = -1;
$prev_position = 0;
$last_line = -1;
/// looping the string stop at each space
while( $position = mb_stripos( $str, " ", ++$position, 'utf-8' ) ) {
if( $position > $last_line + $width + 1 ) {
$formatted.= mb_substr( $str, $last_line + 1, $prev_position - $last_line - 1, 'utf-8' ).$break;
$last_line = $prev_position;
}
$prev_position = $position;
}
/// adding last line without the break
$formatted.= mb_substr( $str, $last_line + 1, mb_strlen( $str ), 'utf-8' );
return $formatted;
}
public function convert_text2image ( $text, $prop )
{
$im = imagecreatetruecolor ( $prop["width"], $prop["height"] );
$rgb = $this->getrgb ( $this->color );
$color = imagecolorallocate ( $im, $rgb["red"], $rgb["green"], $rgb["blue"] );
$img = $this->createtransparent ( $prop["width"], $prop["height"] );
imagettftext ( $img, $this->size, $this->angle, 0, $prop["height"] - abs ( $prop["top"] ), $color, $this->font_face, $text );
return $img;
}
public function check_text_width ( $text )
{
$prop = array();
$bbox = imagettfbbox ( $this->size, $this->angle, $this->font_face, $text );
$prop["left"] = $bbox[0];
$prop["right"] = $bbox[2];
$prop["top"] = $bbox[1];
$prop["bottom"] = $bbox[7];
$padding = 2;
$prop["width"] = abs($prop["left"]) + abs($prop["right"]) + $padding;
$prop["height"] = abs($prop["top"]) + abs($prop["bottom"]) + $padding;
return $prop;
}
/*___| save to image file |___*/
public function save($gdimage, $filename, $attributes=false)
{
$this->type = "png";
$this->chmod = 0777;
$this->bg_color = "#ffffff";
$this->quality = 95;
$this->antialias = true;
$this->setprop ( $attributes );
// process
switch ( strtolower ( $this->type ) )
{
case "jpeg":
case "jpg":
$gdimage = $this->createbackground($gdimage, imagesx($gdimage), imagesy($gdimage));
imagejpeg ( $gdimage, $filename, $this->quality );
break;
case "gif":
$gdimage = $this->createbackground($gdimage, imagesx($gdimage), imagesy($gdimage));
imagegif ( $gdimage, $filename );
break;
case "png":
default :
imagepng ( $gdimage, $filename );
break;
}
chmod ( $filename, $this->chmod );
}
/*___| create gd background image |___*/
public function createbackground($gdimage, $width, $height)
{
$img = imagecreatetruecolor ( $width, $height );
$rgb = $this->getrgb ( $this->bg_color );
$color = imagecolorallocate ( $img, $rgb["red"], $rgb["green"], $rgb["blue"] );
imagefill ( $img, 0, 0, $color );
imagecopyresampled ( $img, $gdimage, 0, 0, 0, 0, $width, $height, $width, $height );
return $img;
}
/*___| create gd transparent image |___*/
public function createtransparent($width, $height)
{
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagesavealpha($img, true);
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefilledrectangle($img, 0, 0, $width, $height, $transparent);
imagecopyresampled($img, $img, 0, 0, 0, 0, $width, $height, $width, $height);
return $img;
}
/*___| load image |___*/
public function createimagefrom($filename, $alpha=true)
{
if ( function_exists ( "exif_imagetype" ) )
{
if ( exif_imagetype ( $filename ) == imagetype_jpeg ) { return $this->createfromjpeg ( $filename ); }
else if ( exif_imagetype ( $filename ) == imagetype_gif ) { return $this->createfromgif ( $filename ); }
else if ( exif_imagetype ( $filename ) == imagetype_png ) { return $this->createfrompng ( $filename, $alpha ); }
}
else
{
if ( strstr ( strtoupper ( $filename ) , ".jpg" ) || strstr ( strtoupper ( $filename ), ".jpeg" )) { return $this->createfromjpeg ( $filename ); }
else if ( strstr ( strtoupper ( $filename ) , ".gif" )) { return $this->createfromgif ( $filename ); }
else if ( strstr ( strtoupper ( $filename ) , ".png" )) { return $this->createfrompng ( $filename, $alpha ); }
}
return false;
}
private function createfromjpeg ( $filename ) { return imagecreatefromjpeg ( $filename ); }
private function createfromgif ( $filename ) { return imagecreatefromgif ( $filename ); }
private function createfrompng ( $filename, $alpha=true )
{
if ( $alpha )
{
list ( $width, $height ) = getimagesize ( $filename );
$png_img = imagecreatefrompng ( $filename );
$img = imagecreatetruecolor ( $width, $height );
imagealphablending ( $img, false );
imagesavealpha ( $img, true );
imagecopyresampled ( $img, $png_img, 0, 0, 0, 0, $width, $height, $width, $height );
} else {
$img = imagecreatefrompng ( $filename );
}
return $img;
}
/*___| attach background image |___*/
public function attachbackgroundimage ( $gdimage, $filename, $attributes=false )
{
$this->x = 0;
$this->y = 0;
$this->setprop ( $attributes );
$img = $this->createimagefrom ( $filename );
imagecopyresampled ( $img, $gdimage, $this->x, $this->y, 0, 0, imagesx($gdimage), imagesy($gdimage), imagesx($gdimage), imagesy($gdimage) );
return $img;
}
/*___| attach image |___*/
public function attachimage ( $source, $target, $filename, $image_attributes=false, $attributes=false )
{
$source_img = $this->createimagefrom ( $source );
$target_img = $this->attachbackgroundimage ( $source_img, $target, $attributes );
$this->save ( $target_img, $filename, $image_attributes );
}
/*___| attach gd image resource |___*/
public function attachgdimage ( $gd_source, $gd_target, $attributes=false )
{
$this->x = 0;
$this->y = 0;
$this->width = 0;
$this->height = 0;
$this->setprop ( $attributes );
imagealphablending($gd_target, true );
imagealphablending($gd_source, true );
imagecopy ( $gd_target, $gd_source, $this->x, $this->y, 0, 0, imagesx($gd_source), imagesy($gd_source) );
return $gd_target;
}
/*___| get rgb color |___*/
public function getrgb($hex)
{
$rgb["red"] = hexdec ( substr ( $hex, 1, 2 ) ) ;
$rgb["green"] = hexdec ( substr ( $hex, 3, 2 ) ) ;
$rgb["blue"] = hexdec ( substr ( $hex, 5, 2 ) ) ;
return $rgb;
}
/*___| set properties |___*/
private function setprop ( $attributes=false )
{
if ( $attributes ) { foreach ( $attributes as $key => $value ) { $k = strtoupper ( $key ); $this->$k = $value; } }
}
}
//调用 方法
$imgresize = new imagetransform();
$imgresize->sourcefile = $source.$file;
$imgresize->targetfile = $destination.$file;
$imgresize->chmodvalue = 0777;
$imgresize->resizetowidth = $tw;
$imgresize->resizetoheight = $th;
$imgresize->jpegoutputquality = 100;
$imgresize->resizeifsmaller = false;
$imgresize->resize();
?>

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor