Last active
December 28, 2015 02:59
-
-
Save Ardakilic/7431389 to your computer and use it in GitHub Desktop.
PHP-TR facebook sayfasından gelen bir soru üzerine can sıkıntısından bir snippet hazırladım. Bu kod resmin canvasını istenen değerde büyütüp altına fontun ebatında ideal ortalanmış ve istenen renkte (örnekte kırmızı) bir şerit atıp altına istenen metni istenen fontla ekliyor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//önce dosya adımız | |
$resim_yolu = 'cat.jpg'; | |
$yeni_yol = 'cat2.jpg'; | |
$alttan_canvas = 100; | |
$font = './arial.ttf'; | |
$fontSize = 20; | |
$metin = 'CAPS VER PAMPA'; | |
//uzantıyı yakalamaya çalışıyoruz dosya adını patlatarak | |
$dosya_dizisi = explode('.',$resim_yolu); | |
$uzanti = $dosya_dizisi[count($dosya_dizisi)-1]; | |
//uzantıya göre image create değişiyor | |
switch($uzanti) { | |
case 'jpg': | |
$resim = imagecreatefromjpeg($resim_yolu); | |
break; | |
case 'png': | |
$resim = imagecreatefrompng($resim_yolu); | |
break; | |
case 'gif': | |
$resim = imagecreatefromgif($resim_yolu); | |
break; | |
default: | |
$resim = imagecreatefromjpeg($resim_yolu); | |
} | |
//width, height alalım: | |
list($genislik, $yukseklik) = getimagesize($resim_yolu); | |
//metnin tam genişlik yüksekliğini alalım ki ideal bir ortalama yapabilelim, | |
//text yüksekliği + 10ar piksel pay kadar canvas artırıcaz | |
$box = imagettfbbox($fontSize,0,$font,$metin); | |
$textGenislik = abs($box[4] - $box[0]); | |
$textYukseklik = abs($box[5] - $box[1]); | |
//soldan ortalama hizası | |
$xKordinat = ($genislik/2)-($textGenislik/2)-2; | |
//dikeyde ortalama hizası, aşağıdan 10 pixel pay verelim | |
$yKordinat = $yukseklik+$textYukseklik+20-10; //(20 pixel total pay, 10 piksel yukardan) | |
$yeniResim = imagecreatetruecolor($genislik, $yukseklik+$textYukseklik+20); | |
//kırmızı ve beyaz rengi tanımlayalım | |
$kirmizi = imagecolorallocate($yeniResim, 255, 0, 0); | |
$beyaz = imagecolorallocate($yeniResim, 255, 255, 255); | |
//resme rengi verelim | |
imagefill($yeniResim,0,0,$kirmizi); | |
imagecopy($yeniResim, $resim, 0, 0, 0, 0, $genislik, $yukseklik); | |
//texti basalım yeni hesaplanan kordinata göre | |
imagettftext($yeniResim, $fontSize, 0 , $xKordinat, $yKordinat, $beyaz, $font, $metin); | |
//uzantıya göre image gösterim değişiyor | |
switch($uzanti) { | |
case 'jpg': | |
imagejpeg($yeniResim,$yeni_yol); | |
break; | |
case 'png': | |
imagepng($yeniResim,$yeni_yol); | |
break; | |
case 'gif': | |
imagegif($yeniResim,$yeni_yol); | |
break; | |
default: | |
imagejpeg($yeniResim,$yeni_yol); | |
} | |
imagedestroy($resim); | |
imagedestroy($yeniResim); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment