PHP
2014.07.10 16:17

Simple GD example

조회 수 7793 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

Resize an image

<?php
// Set the path to the image to resize
$input_image "House.jpg";
// Get the size of the original image into an array
$size getimagesize$input_image );
// Set the new width of the image
$thumb_width "200";
// Calculate the height of the new image to keep the aspect ratio
$thumb_height = ( int )(( $thumb_width/$size[0] )*$size[1] );
// Create a new true color image in the memory
$thumbnail ImageCreateTrueColor$thumb_width$thumb_height );
// Create a new image from file 
$src_img ImageCreateFromJPEG$input_image );
// Create the resized image
ImageCopyResampled$thumbnail$src_img0000$thumb_width$thumb_height$size[0], $size[1] );
// Save the image as resized.jpg
ImageJPEG$thumbnail"resized.jpg" );
// Clear the memory of the tempory image 
ImageDestroy$thumbnail );
?>

Change a png to a jpg

Note: Any transparency is lost in this case as jpg does not support transparency
You can change from different formats by changing the imagecreatefrompng and the imagejpeg functions.

<?php
// Create a new image in the memory from the file 
$png_image imagecreatefrompng"input.png" );
// Save the image as output.jpg
imagejpeg$png_image"output.jpg" );
// Clear the memory of the tempory image 
imagedestroy$png_image );
?>

Crop an image

<?php
// Crop dimensions.
$width 100;
$height 100;
// Set the path to the image to resize
$input_image "House.jpg";
// Get the size of the original image into an array
$size getimagesize$input_image );
// Prepare canvas
$canvas imagecreatetruecolor$width$height );
// Create a new image in the memory from the file 
$cropped imagecreatefromjpeg$input_image );
// Prepare image  crop - center the crop on the image
$newwidth $size[0] / 2;
$newheight $size[1] / 2;
$cropLeft = ( $newwidth/) - ( $width/);
$cropHeight = ( $newheight/) - ( $height/);
// Generate the cropped image
imagecopyresized$canvas$cropped0,0$cropLeft$cropHeight$size[0], $size[1], $newwidth$newheight );
// Save the cropped image as cropped.jpg
imagejpeg$canvas"cropped.jpg" );
// Clear the memory of the tempory images
imagedestroy$canvas );
imagedestroy$cropped );
?>

Rotate an image

<?php
// Set the path to the image to rotate
$input_image "House.jpg";
//How many degrees you wish to rotate
$degrees 180;
// Create the canvas
$canvas imagecreatefromjpeg$input_image ) ;
// Rotates the image
$rotate imagerotate$canvas$degrees) ;
// Save the image as output.jpg
imagejpeg$rotate"output.jpg" );
// Clear the memory of the tempory image
imagedestroy$canvas );
?>

Watermark an image

<?php
// Create the canvas
$canvas imagecreate200100 );  
// Define the colours to use
$black imagecolorallocate$canvas00);  
$white imagecolorallocate$canvas255255255 );  
// Create a rectangle and fill it white
imagefilledrectangle$canvas00200100$white );  
// The path to the font
$font "verdana.ttf"
// The text to use 
$text "House"
// The font size 
$size "30";   
// Set the path to the image to watermark
$input_image "House.jpg"
// Calculate the size of the text 
// If php has been setup without ttf support this will not work.
$box imagettfbbox$size0$font$text );  
$x = (200 - ($box[2] - $box[0])) / 2;  
$y = (100 - ($box[1] - $box[7])) / 2;  
$y -= $box[7];  
// Add the text to the image
imageTTFText$canvas$size0$x$y$black$font$text );  
// Make white transparent
imagecolortransparent $canvas$white );  
// Save the text image as temp.png
imagepng$canvas"temp.png" );  
// Cleanup the tempory image canvas.png
ImageDestroy$canvas );  
// Read in the text watermark image
$watermark imagecreatefrompng"temp.png" );  
// Returns the width of the given image resource  
$watermark_width imagesx$watermark );
//Returns the height of the given image resource    
$watermark_height imagesy$watermark );    
$image imagecreatetruecolor$watermark_width$watermark_height );    
$image imagecreatefromjpeg$input_image );
// Find the size of the original image and read it into an array      
$size getimagesize$input_image );  
// Set the positions of the watermark on the image
$dest_x $size[0] - $watermark_width 100;    
$dest_y $size[1] - $watermark_height 200;    
imagecopymerge($image$watermark$dest_x$dest_y00$watermark_width$watermark_height50);   
// Save the watermarked image as watermarked.jpg 
imagejpeg$image"watermarked.jpg" );
// Clear the memory of the tempory image     
imagedestroy$image );    
imagedestroy$watermark );    
// Delete the text watermark image
unlink"temp.png");  
?>

Create some text as an image

<?php
// Create the canvas
$canvas imagecreate15080 );
// First colour - this will be the default colour for the canvas
$light_blue imagecolorallocate$canvas176226255 );
// The second colour - to be used for the text
$black imagecolorallocate$canvas00);
// Path to the font you are going to use
$font "verdana.ttf";
// Text to write
$text "Text";
// Font size
$size "40";
// Add the text to the canvas
imageTTFText$canvas$size01560$black$font$text );
// Save as Text.jpg
imagejpeg$canvas"Text.jpg" );
// Clear the memory of the tempory image 
ImageDestroy$canvas );
?>

Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
401 일반 배치파일(bat)에서 for루프 사용법 2010.04.15 47717 0
400 일반 배치파일(bat 파일) 명령어 사용법 2007.01.23 42868 61
399 C# 문자열 다루기 1 2012.04.06 18557 0
398 C# 문자열 검색 / 조작 2012.01.03 23480 0
397 Pi 무선랜 wifi 설정 2015.10.01 6837 0
396 LINUX 명령어 뒤의 옵션 자동완성 기능, complete 명령어 2015.01.06 8945 0
395 Android 맨날 까먹는 버튼 클릭 이벤트 핸들러 코드 2014.12.17 5899 0
394 LINUX 매번 잊는다.. tar 풀기 : tar zxvf file 2 2009.04.14 36637 0
393 일반 마크다운 markdown (.md) 파일이란 2017.07.12 12291 0
392 업무 마이크로 버블 Micro Bubble 이란 2016.08.29 9303 0
391 MFC 리사이징 다이얼로그(Resizing dialog) 2008.03.24 46702 0
390 LINUX 리눅스의 기본 명령어들 2015.01.20 6721 0
389 LINUX 리눅스에서 특정작업 or 명령어 반복하기 Crontab 2014.11.10 10332 0
388 LINUX 리눅스 파일시스템 체크 하기 fsck(e2fsck) 사용법 2016.04.15 39746 0
387 LINUX 리눅스 커널리빌딩, 버젼확인, ctags명령, vi편집기활용, 기본명령 2012.05.07 14962 0
목록
Board Pagination ‹ Prev 1 ... 3 4 5 6 7 8 9 10 11 12 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5