2013-06-30 2 views
0

이미지를 저장하지 않고 php-image-magician을 사용하여 업로드 된 이미지와 텍스트 워터 마크를 다른 크기로 표시하려고합니다.PHP 이미지 마술사를 사용하여 이미지 크기 조정

여기에 코드입니다 :

<?php 

$errors = array(); 

function mimeValidator($imagefile) 
{ 
$imageInfo = getimagesize($imagefile); 
$allowedMimes = array('image/jpg', 'image/jpeg', 'image/png'); 

if(!in_array($imageInfo['mime'], $allowedMimes)) 
$errors[] = "Only jpg and png images are supported"; 
} 

function renderImage($imageSource, $watermark, $fontSize, $font) 
{ 
$imageInfo = getimagesize($imageSource); 
list($width,$height) = getimagesize($imageSource); 

if($imageInfo['mime'] == 'image/jpg' || $imageInfo['mime'] == 'image/jpeg') 
$imageSource = imagecreatefromjpeg($imageSource); 
elseif($imageInfo['mime'] == 'image/png') 
$imageSource = imagecreatefrompng($imageSource); 

$image = imagecreatetruecolor($width, $height); 
$blue = imagecolorallocate($image, 79, 166, 185); 
$text = imagettftext($imageSource, $fontSize, 0, 20, ($height-20), $blue, $font,  $watermark); 

ob_start(); 
imagejpeg($imageSource); 
$image = ob_get_contents(); 
ob_end_clean(); 

return base64_encode($image); 
} 

?><html> 
<head> 
<title> 
image Watermark and resize 
</title> 

<style type="text/css"> 
body{ width:800px; margin: 15px auto; padding:0px; font-family: arial}  

</style> 
</head> 
<body> 
<form name="imageUpload" id="imageUpload" method="post" enctype="multipart/form-data" > 
<fieldset> 
<legend>Image text watermark</legend> 
Text: <input type="text" name="text" id="text"/><br /> 
Image: <input type="file" name="image" id="image"/><br /> 
<input type="submit" name="submit" id="createmark" value="Submit" /> 
</fieldset> 
<?php 

if(isset($_POST['submit'])) 
{ 
if($_POST['text'] == '') 
$errors[] = "Text is too short"; 

if($_FILES['image']['tmp_name'] == '') 
$errors[] = "Please upload a image"; 
else 
mimeValidator($_FILES['image']['tmp_name']); 

if(count($errors) == 0) 
       { 
include('php-image-magician/php_image_magician.php'); 

$magicianObj = new imageLib($_FILES["image"]["tmp_name"]); 
$magicianObj -> resizeImage(100, 200); 
$magicianObj -> saveImage($imagename); 
echo "<img src=\"data:image/gif;base64," . renderImage($_FILES["image"]["tmp_name"], $_POST['text'], 30, "./arial.ttf") . "\" />"; 
} 
else 
{ 
echo "<ul>"; 

foreach($errors as $error) 
echo "<li>{$error}</li>"; 

echo "</ul>"; 
} 

} 
?> 
</form> 
</body> 
</html> 

내가 오류 "파일을 C : \ XAMPP \ tmp에 php9E3D.tmp \ 없거나 잘못되었습니다"얻고 난이의 newbee입니다 합니다. 도와주세요.

답변

1

나는 php-image-magician을 한번도 사용하지 않았기 때문에 다음 정보가 잘못된 정보 일 수 있습니다. 내가 보는 것과

:

$magicianObj = new imageLib($_FILES["image"]["tmp_name"]); 

내부적으로 imageLib 전화 차례로 다음을 수행 openImage : 1) 파일이 실제로 확장을 검사) 이 존재하고 올바른 imagecreatefrom_를 사용하는 경우는 확인 _ _ function.

여기가 문제가되는 곳입니다. 웹 서버에 파일을 업로드하면 임시 파일 이름으로 저장됩니다 (예 : "php9E3D.tmp"). 이 확장자 (.tmp)는 php-image-magician (.jpg, .jpeg, .gif, .png, .bmp, .psd)의 허용 된 확장 목록에 없습니다. 따라서 내부 image 변수가 false로 설정되고 전체 라이브러리가 작동하지 않습니다.

당신은이 문제를 해결하기위한 몇 가지 옵션이 있습니다 :

1) (특히 openImage)이 있지만 사용하지 않는 것이 좋습니다 PHP 이미지-마술사를 다시 작성합니다.

2) move_uploaded_file을 사용하여 업로드 한 파일의 이름을 변경하십시오 (예 2 here 참조).

3) PHP 이미지-마술사에 의존성을 제거하고 PHPs를 사용하기 내부 기능 (imagescale과 친구, 당신은 이미

희망이 도움이됩니다) 당신의 renderImage 기능에서 작업 할 이미지 리소스를 만드는 .

+0

전화하세요. 거기에 PHP를위한 많은 크기 조정 스크립트가 제공 될 수 있습니다. 실제로는 잘 작성되었습니다. 그냥 간단한 Google 검색을 수행하십시오. – OptimusCrime