combining code from this website and adding a little to my own, i have figured it out. enjoy.
function updateThumb[$image, $newColor] {
$img = imagecreatefrompng[$image];
$w = imagesx[$img];
$h = imagesy[$img];
// Work through pixels
for[$y=0;$y255]$green_set=255;
if[$blue_set>255]$blue_set=255;
$pixelColor = imagecolorallocatealpha[$img, $red_set, $green_set, $blue_set, $rgb['alpha']];
imagesetpixel [$img, $x, $y, $pixelColor];
}
}
// Restore Alpha
imageAlphaBlending[$img, true];
imageSaveAlpha[$img, true];
return $img;
}
function makeThumb[$path, $top, $bottom=FALSE] {
$width = imagesx[$top];
$height = imagesy[$top];
$thumbHeight = $bottom != FALSE ? $height * 2 : $height;
// Create Transparent PNG
$thumb = imagecreatetruecolor[$width, $thumbHeight];
$transparent = imagecolorallocatealpha[$thumb, 0, 0, 0, 127];
imagefill[$thumb, 0, 0, $transparent];
// Copy Top Image
imagecopy[$thumb, $top, 0, 0, 0, 0, $width, $height];
// Copy Bottom Image
if [$bottom != FALSE] {
imagecopy[$thumb, $bottom, 0, $height, 0, 0, $width, $height];
}
// Save Image with Alpha
imageAlphaBlending[$thumb, true];
imageSaveAlpha[$thumb, true];
header['Content-Type: image/png'];
imagepng[$thumb, $path]; // save image as png
}
$thumbTop = updateThumb['input/path', array[240,105,15]];
Hàm hủy [destructor] được gọi khi đối tượng bị hủy hoặc PHP script bị dừng hoặc thoát. Trong PHP, hàm
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
9 là hàm hủy [destructor] của class trong PHP.Hàm hủy [destructor] trong PHP thường được dùng để giải phóng bộ nhớ của đối tượng khi không sử dụng nữa.Show
- Java collections framework: Map interface và lớp HashMap
- Chúng ta có thể định nghĩa hàm
4 không có tham số trong class trong PHP. Lúc này, hàm khởi tạo này có chức năng giống như hàm khởi tạo mặc định nhưng có thể định nghĩa các dòng code trong thân hàm để thực thi lúc tạo đối tượng.// Load the Black Circle PNG image $png = imagecreatefrompng[ 'mask.png' ]; $width = imagesx[ $png ]; $height = imagesy[ $png ]; // Create a mask image $mask = imagecreatetruecolor[ $width, $height ]; // We'll use Magenta as our new transparent colour - set it as the solid background colour. $magenta = imagecolorallocate[ $mask, 255, 0, 255 ]; imagefill[ $mask, 0, 0, $magenta ]; // Copy the png image onto the mask. Destroy it to free up memory. imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ]; imagedestroy[ $png ]; // Set the black portion of the mask to transparent. $black = imagecolorallocate[ $mask, 0, 0, 0 ]; imagecolortransparent[ $mask, $black ]; // Load JPEG image. $jpg = imagecreatefromjpeg[ 'image.jpg' ]; $j_width = imagesx[ $jpg ]; $j_height = imagesx[ $jpg ]; // Enable alpha blending and copy the png image imagealphablending[ $jpg, true ]; imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ]; imagedestroy[ $mask ]; // Set the new transparent colour and output new image to browser as a png. $magenta = imagecolorallocate[ $jpg, 255, 0, 255 ]; imagecolortransparent[ $jpg, $magenta ]; imagepng[ $jpg ];
- Chúng ta có thể định nghĩa hàm
4 có 1 hoặc nhiều tham số. Hàm khởi tạo có tham số thường được sử dụng để khởi tạo các giá trị cho các thuộc tính của đối tượng.// Load the Black Circle PNG image $png = imagecreatefrompng[ 'mask.png' ]; $width = imagesx[ $png ]; $height = imagesy[ $png ]; // Create a mask image $mask = imagecreatetruecolor[ $width, $height ]; // We'll use Magenta as our new transparent colour - set it as the solid background colour. $magenta = imagecolorallocate[ $mask, 255, 0, 255 ]; imagefill[ $mask, 0, 0, $magenta ]; // Copy the png image onto the mask. Destroy it to free up memory. imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ]; imagedestroy[ $png ]; // Set the black portion of the mask to transparent. $black = imagecolorallocate[ $mask, 0, 0, 0 ]; imagecolortransparent[ $mask, $black ]; // Load JPEG image. $jpg = imagecreatefromjpeg[ 'image.jpg' ]; $j_width = imagesx[ $jpg ]; $j_height = imagesx[ $jpg ]; // Enable alpha blending and copy the png image imagealphablending[ $jpg, true ]; imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ]; imagedestroy[ $mask ]; // Set the new transparent colour and output new image to browser as a png. $magenta = imagecolorallocate[ $jpg, 255, 0, 255 ]; imagecolortransparent[ $jpg, $magenta ]; imagepng[ $jpg ];
- Chúng ta có thể định nghĩa hàm
4 không có tham số trong class trong PHP. Lúc này, hàm khởi tạo này có chức năng giống như hàm khởi tạo mặc định nhưng có thể định nghĩa các dòng code trong thân hàm để thực thi lúc tạo đối tượng.// Load the Black Circle PNG image $png = imagecreatefrompng[ 'mask.png' ]; $width = imagesx[ $png ]; $height = imagesy[ $png ]; // Create a mask image $mask = imagecreatetruecolor[ $width, $height ]; // We'll use Magenta as our new transparent colour - set it as the solid background colour. $magenta = imagecolorallocate[ $mask, 255, 0, 255 ]; imagefill[ $mask, 0, 0, $magenta ]; // Copy the png image onto the mask. Destroy it to free up memory. imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ]; imagedestroy[ $png ]; // Set the black portion of the mask to transparent. $black = imagecolorallocate[ $mask, 0, 0, 0 ]; imagecolortransparent[ $mask, $black ]; // Load JPEG image. $jpg = imagecreatefromjpeg[ 'image.jpg' ]; $j_width = imagesx[ $jpg ]; $j_height = imagesx[ $jpg ]; // Enable alpha blending and copy the png image imagealphablending[ $jpg, true ]; imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ]; imagedestroy[ $mask ]; // Set the new transparent colour and output new image to browser as a png. $magenta = imagecolorallocate[ $jpg, 255, 0, 255 ]; imagecolortransparent[ $jpg, $magenta ]; imagepng[ $jpg ];
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
1.2. Hàm khởi tạo có tham số [parameterized constructor]
// Load JPEG Image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$width = imagesx[ $jpg ];
$height = imagesx[ $jpg ];
// Create mask at same size with an opaque background.
$mask = imagecreatetruecolor[ $width, $height ];
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Disable alpha blending and draw a transparent shape onto the mask.
$transparent = imagecolorallocatealpha[ $mask, 255, 255, 255, 127 ];
imagealphablending[ $mask, false ];
imagefilledellipse[ $mask, round[ $width / 2 ], round[ $height / 2 ], $width, $height, $transparent ];
// Paste the mask onto the original image and set the new transparent colour.
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $mask ];
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
// Output new image to browser as a png.
imagepng[ $jpg ];
Chúng ta có thể định nghĩa hàm
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 có 1 hoặc nhiều tham số. Hàm khởi tạo có tham số thường được sử dụng để khởi tạo các giá trị cho các thuộc tính của đối tượng.
0 bình luận chia sẻ Tom cat has grey and white color
Lưu ý: Trong PHP, chỉ được định nghĩa một hàm khởi tạo
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 trong class.constructor và destructor của class trong PHP. Để học tốt bài này, các bạn cần đọc lại bài Cài đặt môi trường lập trình Web PHP với XAMPP để biết cách chạy ứng dụng web PHP với XAMPP.1.3. Hàm khởi tạo sao chép [copy constructor]
- Java collections framework: Map interface và lớp HashMap
- Chúng ta có thể định nghĩa hàm
4 không có tham số trong class trong PHP. Lúc này, hàm khởi tạo này có chức năng giống như hàm khởi tạo mặc định nhưng có thể định nghĩa các dòng code trong thân hàm để thực thi lúc tạo đối tượng.// Load the Black Circle PNG image $png = imagecreatefrompng[ 'mask.png' ]; $width = imagesx[ $png ]; $height = imagesy[ $png ]; // Create a mask image $mask = imagecreatetruecolor[ $width, $height ]; // We'll use Magenta as our new transparent colour - set it as the solid background colour. $magenta = imagecolorallocate[ $mask, 255, 0, 255 ]; imagefill[ $mask, 0, 0, $magenta ]; // Copy the png image onto the mask. Destroy it to free up memory. imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ]; imagedestroy[ $png ]; // Set the black portion of the mask to transparent. $black = imagecolorallocate[ $mask, 0, 0, 0 ]; imagecolortransparent[ $mask, $black ]; // Load JPEG image. $jpg = imagecreatefromjpeg[ 'image.jpg' ]; $j_width = imagesx[ $jpg ]; $j_height = imagesx[ $jpg ]; // Enable alpha blending and copy the png image imagealphablending[ $jpg, true ]; imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ]; imagedestroy[ $mask ]; // Set the new transparent colour and output new image to browser as a png. $magenta = imagecolorallocate[ $jpg, 255, 0, 255 ]; imagecolortransparent[ $jpg, $magenta ]; imagepng[ $jpg ];
- Chúng ta có thể định nghĩa hàm
4 có 1 hoặc nhiều tham số. Hàm khởi tạo có tham số thường được sử dụng để khởi tạo các giá trị cho các thuộc tính của đối tượng.// Load the Black Circle PNG image $png = imagecreatefrompng[ 'mask.png' ]; $width = imagesx[ $png ]; $height = imagesy[ $png ]; // Create a mask image $mask = imagecreatetruecolor[ $width, $height ]; // We'll use Magenta as our new transparent colour - set it as the solid background colour. $magenta = imagecolorallocate[ $mask, 255, 0, 255 ]; imagefill[ $mask, 0, 0, $magenta ]; // Copy the png image onto the mask. Destroy it to free up memory. imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ]; imagedestroy[ $png ]; // Set the black portion of the mask to transparent. $black = imagecolorallocate[ $mask, 0, 0, 0 ]; imagecolortransparent[ $mask, $black ]; // Load JPEG image. $jpg = imagecreatefromjpeg[ 'image.jpg' ]; $j_width = imagesx[ $jpg ]; $j_height = imagesx[ $jpg ]; // Enable alpha blending and copy the png image imagealphablending[ $jpg, true ]; imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ]; imagedestroy[ $mask ]; // Set the new transparent colour and output new image to browser as a png. $magenta = imagecolorallocate[ $jpg, 255, 0, 255 ]; imagecolortransparent[ $jpg, $magenta ]; imagepng[ $jpg ];
- Chúng ta có thể định nghĩa hàm
4 không có tham số trong class trong PHP. Lúc này, hàm khởi tạo này có chức năng giống như hàm khởi tạo mặc định nhưng có thể định nghĩa các dòng code trong thân hàm để thực thi lúc tạo đối tượng.// Load the Black Circle PNG image $png = imagecreatefrompng[ 'mask.png' ]; $width = imagesx[ $png ]; $height = imagesy[ $png ]; // Create a mask image $mask = imagecreatetruecolor[ $width, $height ]; // We'll use Magenta as our new transparent colour - set it as the solid background colour. $magenta = imagecolorallocate[ $mask, 255, 0, 255 ]; imagefill[ $mask, 0, 0, $magenta ]; // Copy the png image onto the mask. Destroy it to free up memory. imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ]; imagedestroy[ $png ]; // Set the black portion of the mask to transparent. $black = imagecolorallocate[ $mask, 0, 0, 0 ]; imagecolortransparent[ $mask, $black ]; // Load JPEG image. $jpg = imagecreatefromjpeg[ 'image.jpg' ]; $j_width = imagesx[ $jpg ]; $j_height = imagesx[ $jpg ]; // Enable alpha blending and copy the png image imagealphablending[ $jpg, true ]; imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ]; imagedestroy[ $mask ]; // Set the new transparent colour and output new image to browser as a png. $magenta = imagecolorallocate[ $jpg, 255, 0, 255 ]; imagecolortransparent[ $jpg, $magenta ]; imagepng[ $jpg ];
1.2. Hàm khởi tạo có tham số [parameterized constructor] luôn luôn được gọi khi một đối tượng của một lớp được tạo ra. Hàm khởi tạo được sử dụng để khởi tạo giá trị cho các thuộc tính của đối tượng hoặc thực hiện một số thao tác khi đối tượng đang được tạo ra.
Chúng ta có thể định nghĩa hàm
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 có 1 hoặc nhiều tham số. Hàm khởi tạo có tham số thường được sử dụng để khởi tạo các giá trị cho các thuộc tính của đối tượng.// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 là hàm khởi tạo [constructor] của class trong PHP. Có 3 dạng hàm khởi tạo là:Tom cat has grey and white color
- Lưu ý: Trong PHP, chỉ được định nghĩa một hàm khởi tạo
4 trong class.// Load the Black Circle PNG image $png = imagecreatefrompng[ 'mask.png' ]; $width = imagesx[ $png ]; $height = imagesy[ $png ]; // Create a mask image $mask = imagecreatetruecolor[ $width, $height ]; // We'll use Magenta as our new transparent colour - set it as the solid background colour. $magenta = imagecolorallocate[ $mask, 255, 0, 255 ]; imagefill[ $mask, 0, 0, $magenta ]; // Copy the png image onto the mask. Destroy it to free up memory. imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ]; imagedestroy[ $png ]; // Set the black portion of the mask to transparent. $black = imagecolorallocate[ $mask, 0, 0, 0 ]; imagecolortransparent[ $mask, $black ]; // Load JPEG image. $jpg = imagecreatefromjpeg[ 'image.jpg' ]; $j_width = imagesx[ $jpg ]; $j_height = imagesx[ $jpg ]; // Enable alpha blending and copy the png image imagealphablending[ $jpg, true ]; imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ]; imagedestroy[ $mask ]; // Set the new transparent colour and output new image to browser as a png. $magenta = imagecolorallocate[ $jpg, 255, 0, 255 ]; imagecolortransparent[ $jpg, $magenta ]; imagepng[ $jpg ];
- 1.3. Hàm khởi tạo sao chép [copy constructor]
Java collections framework: Map interface và lớp HashMap
Nếu trong một class không định nghĩa hàm
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 thì PHP sẽ tự động tạo ra một hàm khởi tạo mặc định. Hàm khởi tạo mặc định không có tham số và không có dòng code nào trong thân hàm. Khi gọi hàm khởi tạo mặc định để tạo một đối tượng thì các thuộc tính của đối tượng sẽ nhận giá trị mặc định.class không định nghĩa hàm
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 thì PHP sẽ tự động tạo ra một hàm khởi tạo mặc định. Hàm khởi tạo mặc định không có tham số và không có dòng code nào trong thân hàm. Khi gọi hàm khởi tạo mặc định để tạo một đối tượng thì các thuộc tính của đối tượng sẽ nhận giá trị mặc định.
Kết quảcat has color
Chúng ta có thể định nghĩa hàm
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 không có tham số trong class trong PHP. Lúc này, hàm khởi tạo này có chức năng giống như hàm khởi tạo mặc định nhưng có thể định nghĩa các dòng code trong thân hàm để thực thi lúc tạo đối tượng.// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 không có tham số trong class trong PHP. Lúc này, hàm khởi tạo này có chức năng giống như hàm khởi tạo
mặc định nhưng có thể định nghĩa các dòng code trong thân hàm để thực thi lúc tạo đối tượng.
Kết quảThis is constructor function
cat has color
Chúng ta có thể định nghĩa hàm // Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 không có tham số trong class trong PHP. Lúc này, hàm khởi tạo này có chức năng giống như hàm khởi tạo mặc định nhưng có thể định nghĩa các dòng code trong thân hàm để thực thi lúc tạo đối tượng.
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
1.2. Hàm khởi tạo có tham số [parameterized constructor]
4 có 1 hoặc nhiều tham số. Hàm khởi tạo có tham số thường được sử dụng để khởi tạo các giá trị cho các thuộc tính của đối tượng.// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
Kết quảChúng ta có thể định nghĩa hàm // Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 không có tham số trong class trong PHP. Lúc này, hàm khởi tạo này có chức năng giống như hàm khởi tạo mặc định nhưng có thể định nghĩa các dòng code trong thân hàm để thực thi lúc tạo đối tượng.1.2. Hàm khởi tạo có tham số [parameterized constructor]: Trong PHP, chỉ được định nghĩa một hàm khởi tạo
4 trong class.// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
Chúng ta có thể định nghĩa hàm // Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
4 có 1 hoặc nhiều tham số. Hàm khởi tạo có tham số thường được sử dụng để khởi tạo các giá trị cho các thuộc tính của đối tượng.
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
Hàm khởi tạo sao chép được sử dụng để tạo ra một bản sao chép [copy] của một đối tượng đã có. Hàm này lấy địa chỉ của một đối tượng khác làm tham số. Trong PHP, hàm này bản chất là một hàm sao chép thuộc tính của đối tượng này cho đối tượng khác.
Kết quả// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
02. Hàm hủy [destructor] của class trong PHP
Hàm hủy [destructor] được gọi khi đối tượng bị hủy hoặc PHP script bị dừng hoặc thoát. Trong PHP, hàm
9 là hàm hủy [destructor] của class trong PHP. được gọi khi đối tượng bị hủy hoặc PHP script bị dừng hoặc thoát. Trong PHP, hàm // Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
9 là hàm hủy [destructor] của class trong PHP.// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
1Kết quả// Load the Black Circle PNG image
$png = imagecreatefrompng[ 'mask.png' ];
$width = imagesx[ $png ];
$height = imagesy[ $png ];
// Create a mask image
$mask = imagecreatetruecolor[ $width, $height ];
// We'll use Magenta as our new transparent colour - set it as the solid background colour.
$magenta = imagecolorallocate[ $mask, 255, 0, 255 ];
imagefill[ $mask, 0, 0, $magenta ];
// Copy the png image onto the mask. Destroy it to free up memory.
imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ];
imagedestroy[ $png ];
// Set the black portion of the mask to transparent.
$black = imagecolorallocate[ $mask, 0, 0, 0 ];
imagecolortransparent[ $mask, $black ];
// Load JPEG image.
$jpg = imagecreatefromjpeg[ 'image.jpg' ];
$j_width = imagesx[ $jpg ];
$j_height = imagesx[ $jpg ];
// Enable alpha blending and copy the png image
imagealphablending[ $jpg, true ];
imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ];
imagedestroy[ $mask ];
// Set the new transparent colour and output new image to browser as a png.
$magenta = imagecolorallocate[ $jpg, 255, 0, 255 ];
imagecolortransparent[ $jpg, $magenta ];
imagepng[ $jpg ];
22. Hàm hủy [destructor] của class trong PHP
- Hàm hủy [destructor] được gọi khi đối tượng bị hủy hoặc PHP script bị dừng hoặc thoát. Trong PHP, hàm
9 là hàm hủy [destructor] của class trong PHP.// Load the Black Circle PNG image $png = imagecreatefrompng[ 'mask.png' ]; $width = imagesx[ $png ]; $height = imagesy[ $png ]; // Create a mask image $mask = imagecreatetruecolor[ $width, $height ]; // We'll use Magenta as our new transparent colour - set it as the solid background colour. $magenta = imagecolorallocate[ $mask, 255, 0, 255 ]; imagefill[ $mask, 0, 0, $magenta ]; // Copy the png image onto the mask. Destroy it to free up memory. imagecopyresampled[ $mask, $png, 0, 0, 0, 0, $width, $height, $width, $height ]; imagedestroy[ $png ]; // Set the black portion of the mask to transparent. $black = imagecolorallocate[ $mask, 0, 0, 0 ]; imagecolortransparent[ $mask, $black ]; // Load JPEG image. $jpg = imagecreatefromjpeg[ 'image.jpg' ]; $j_width = imagesx[ $jpg ]; $j_height = imagesx[ $jpg ]; // Enable alpha blending and copy the png image imagealphablending[ $jpg, true ]; imagecopyresampled[ $jpg, $mask, 0, 0, 0, 0, $j_width, $j_height, $width, $height ]; imagedestroy[ $mask ]; // Set the new transparent colour and output new image to browser as a png. $magenta = imagecolorallocate[ $jpg, 255, 0, 255 ]; imagecolortransparent[ $jpg, $magenta ]; imagepng[ $jpg ];
- Hàm hủy [destructor] trong PHP thường được dùng để giải phóng bộ nhớ của đối tượng khi không sử dụng nữa.
- Java collections framework: Map interface và lớp HashMap
- Chương trình Java tìm số ngày của tháng trong một năm
- Thêm [insert] dữ liệu trong MySQL với PHP