2011-07-26 6 views
-3

확인. 나는 그 같은한 변수에서 다른 변수로 이미지 크기 조정

$ IMG = imagecreatefromjpeg ($ SRCFILE)를 생성하는 것입니다 생각 실제로 사진

있는 변수가;

이제는 이미지 너비를 550으로 조정하고 싶지만 동일하게 유지해야하는 가로 세로 비율을 잃지는 않습니다.

그런 다음 결과 이미지를 변수에 다시 등록해야 나머지 코드가 텍스트 삽입 등을 할 수 있습니다.

사이트 demotivation-fakeposters 및 기타를 만들고 싶습니다.

러시아어에서 일부 코드가 발견되었지만 크기 조정이 작동하지 않아 DataLife Engine과 코드로 전체 사이트에서 포스터와 사용자가 생성 및 게시 할 수 있다는 데 동의하게되었습니다. 이 몇 가지 작은 상형 문자가 있지만이 러시아 키릴 문자로 인해

(모든뿐만 아니라 의미가 큰 경우 이미지를 감소하기위한)

여기가입니다. if (oldwidth > oldheight) ratio = newwidth/oldwidth else ratio = newheight/oldheight :

<?php 

    if (!defined("DATALIFEENGINE")) 
    { 
    die("Hacking Attemp!"); 
    } 

    function win_to_utf($s) 
{ 
for($i=0, $m=strlen($s); $i<$m; $i++) 
{ 
$c=ord($s[$i]); 
if ($c<=127) 
{$t.=chr($c); continue; } 
if ($c>=192 && $c<=207) 
{$t.=chr(208).chr($c-48); continue; } 
if ($c>=208 && $c<=239) 
{$t.=chr(208).chr($c-48); continue; } 
if ($c>=240 && $c<=255) 
{$t.=chr(209).chr($c-112); continue; } 
if ($c==184) { $t.=chr(209).chr(209); 
continue; }; 
if ($c==168) { $t.=chr(208).chr(129); 
continue; }; 
} 
return $t; 
} 
    $dirdem=ROOT_DIR.'/uploads/demotivation/'; 
$tpl->load_template('demgen.tpl'); 
if (isset($_FILES["file"])){ 
$myfile = $_FILES["file"]["tmp_name"]; 
$font = str_replace ("\\", "/", getcwd())."/arial.ttf"; 
$font1 = $font; 
$headerSize = 33.5; 
$textSize = 24.0; 
$imageMargin = 20; 
    $spacing = 16; 
    $borderPad = 16; 
$srcFile = $myfile; 

$header = win_to_utf($_POST["head"]); 
    $text = win_to_utf($_POST["text"]); 
if(!file_exists($srcFile)){ 
    echo "picture file not found\n"; 
    exit; 
    } 
    $img = imagecreatefromjpeg($srcFile); 
    list($w, $h) = getimagesize($srcFile); 

    $ha1 = imagettfbbox($headerSize, 0, $font1, $header); 
    $ha2 = imagettfbbox($textSize, 0,$font, $text); 

    $wt1 = $ha1[2]-$ha1[0]; 
    $ht1 = $ha1[1]-$ha1[7]; 
    $wt2 = $ha2[2]-$ha2[0]; 
    $ht2 = $ha2[1]-$ha2[7]; 

    $fw = $w + 8 + $imageMargin * 2 + $borderPad * 2; 
    $fh = $h + 8 + $imageMargin * 2 + $borderPad * 2 + $ht1 + $ht2 + $spacing * 3; 
    $fon = imagecreatetruecolor($fw, $fh); 

    $white = imagecolorallocate($fon, 255, 255, 255); 
    $black = imagecolorallocate($fon, 0, 0, 0); 
    imagefill($fon, 0, 0, $black); 

    imagecopy($fon, $img, $imageMargin + $borderPad + 4, $imageMargin + $borderPad + 4, 0, 0, $w, $h); 

    imagerectangle($fon, $imageMargin + $borderPad + 4 - 3, $imageMargin + $borderPad + 4 - 3, 
      $imageMargin + $borderPad + $w + 4 + 2, $imageMargin + $borderPad + $h + 4 + 2, $white); 
    imagerectangle($fon, $imageMargin + $borderPad + 4 - 4, $imageMargin + $borderPad + 4 - 4, 
      $imageMargin + $borderPad + $w + 4 + 3, $imageMargin + $borderPad + $h + 4 + 3, $white); 

    imagettftext($fon, $headerSize, 0, 
    ($fw - $wt1)/2, $imageMargin + $borderPad*2 + 8 + $h + $ht1 + $spacing, 
    $white, $font1, $header); 

    imagettftext($fon, $textSize, 0, 
    ($fw - $wt2)/2, $imageMargin + $borderPad*2 + 8 + $h + $ht1 + $ht2 + $spacing*2, 
    $white, $font, $text); 
$ts=$dirdem.time(); 
$ts1='/uploads/demotivation/'.time(); 
    imagejpeg($fon, $ts."_demotiv.jpg", 98); 
    imagecolordeallocate($fon, $black); 
    imagecolordeallocate($fon, $white); 
    imagedestroy($fon); 
$output=$ts1."_demotiv.jpg"; 
$wdr= <<<HTML 
<center> 
    <form enctype="multipart/form-data"> 
     <tr><td width="120" height="30"><span style="font-size: xx-small;">» Ïðÿìàÿ ññûëêà:</span></td><td><input type="text" name="head" value="http://{$_SERVER['HTTP_HOST']}{$output}" style="border:1px solid #ccc;height:16px;width:300px;padding-left:5px;"></td></tr> 
    <tr><td width="120" height="40"><span style="font-size: xx-small;">» Êîä äëÿ ôîðóìà:</span></td><td><input type="text" name="head" value="[img]http://{$_SERVER['HTTP_HOST']}{$output}[/img]" style="border:1px solid #ccc;height:16px;width:300px;padding-left:5px;"></td></tr> 
    <tr><td width="120" height="30"><span style="font-size: xx-small;">» HTML-êîä:</span></td><td><input type="text" name="head" value="&lt;img src=&quot;http://{$_SERVER['HTTP_HOST']}{$output}&quot;&gt;" style="border:1px solid #ccc;height:16px;width:300px;padding-left:5px;"></td></tr> 
</form> 
</center> 
HTML; 
} else { 
$wdr = <<<HTML 
<center> 
    <form method="post" enctype="multipart/form-data"> 
<tr><td width="120" height="40"><span style="font-size: xx-small;">» <b>Øàã 1:</b> Èçîáðàæåíèå:</span></td><td><input type="file" name="file"></td></tr> 
<tr><td width="120" height="30"><span style="font-size: xx-small;">» <b>Øàã 2:</b> Íàäïèñü ââåðõó:</span></td><td><input type="text" value="Ââåäèòå íàäïèñü..." onblur="if(this.value=='') this.value='Ââåäèòå íàäïèñü...';" onfocus="if(this.value=='Ââåäèòå íàäïèñü...') this.value='';" name="head" style="border:1px solid #ccc;height:16px;width:165px;padding-left:5px;"></td></tr> 
<tr><td width="120" height="30"><span style="font-size: xx-small;">» <b>Øàã 3:</b> Íàäïèñü âíèçó:</span></td><td><input type="text" value="Ââåäèòå íàäïèñü..." onblur="if(this.value=='') this.value='Ââåäèòå íàäïèñü...';" onfocus="if(this.value=='Ââåäèòå íàäïèñü...') this.value='';" name="text" style="border:1px solid #ccc;height:16px;width:165px;padding-left:5px;" value=""></td></tr> 
</table><br> 
<center><input type="submit" name="submit" value="Ñîçäàòü" style="border:1px solid #ccc;height:22px;width:200px;padding-left:5px;"></center> 
</form> 
</center> 
HTML; 

} 
$tpl->set('{cnt}', $wdr); 

    $tpl->compile('content'); 
    $tpl->clear(); 
?> 

답변

0

나는 당신이는 x의 큰 또는 y 축이 비율의 기준으로 사용되는 비율을 의미 생각합니다. 복사 된 이미지의 최대 허용 너비와 높이를 고려해야합니다. PHP GD 함수를 사용하면 이렇게 할 수 있습니다 ...

$imageFilePath = '/original.jpg'; // path to original jpg 
$imageSource = imagecreatefromjpeg($imageFilePath); 
$newImageFilePath = '/new.jpg'; // path to new resized image name 
$maxWidth = 100; // the max width you allow 
$maxHeight = 100; // the max height you allow 
$imgInfo = getimagesize($imageFilePath); 
$originalWidth = (int) $imgInfo[0]; 
$originalHeight = (int) $imgInfo[1]; 
$originalType = (int) $imgInfo[2]; 
$conversionRatio = (($originalWidth > $originalHeight) ? $maxWidth/$originalWidth : $maxHeight/$originalHeight); // the important part 
$newWidth = $originalWidth * $conversionRatio; 
$newHeight = $originalHeight * $conversionRatio; 
$newImage = imagecreatetruecolor($newWidth, $newHeight); 
imagecopyresampled($newImage, $imageSource, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); 
imagejpeg($newImage, $newImageFilePath); 
+0

하지만 사진을 저장하고 싶지 않습니다. 코드를 편집 할 수 있도록 크기를 조정하고 다른 변수에 써야합니다. – John888