PDA

View Full Version : Không xem hình được khi download về



quanghien2005k
20-12-2013, 10:25
Xin chào mọi người

Mình có lỗi này xin mọi người giúp đỡ. Mình muốn cho user khi vào xem hay download một hình thì add cái userid của họ vào hình(user đã login). Mình code ở local thì chạy bình thường nhưng khi đưa lên host thì xem hình thì được, còn khi down hình về thì không xem được.

Ai biết lỗi gì xin chỉ cho mình với, cám ơn mọi người

Đây là code của mình.

File add text vào hình: picture.php

$string = $_GET["uid"];
$fontsize = 14;
$fontfamily = "fonts/arial.ttf";
$angle=0;
$file = $_GET["img"];
$output = "";

//get extension file
$path_info = pathinfo($file);
$extension = $path_info['extension'];
//check extension picture and choice method create picture
switch($extension) {
case "png":
$output = "image/png";
$im = imagecreatefrompng($file);
break;
case "jpg":
case "jpeg":
$output = "image/jpeg";
$im = imagecreatefromjpeg($file);
break;
case "gif":
$output = "image/gif";
$im = imagecreatefromgif($file);
break;
}
//set position x and y
$height = imagefontheight($fontsize) ;//get hieght text
$text_width = imagefontwidth($fontsize)*strlen($string);
$center = ceil(imagesx($im) / 2);
$x = imagesx($im) - 30;
$y = imagesy($im) - $height;

//sset font color
$textColor = imagecolorallocate ($im, 255, 255,255);

//Draw text to picture
imagettftext($im, $fontsize, $angle, $x, $y, $textColor, $fontfamily, $string);

header ("Content-type: " . $output );
//Out picture to browse
imagepng($im);
//Destroy an image
imagedestroy($im);


Code download hình

$fileOriginal = "http://localhost/joomla_2.5.3/picture.php?uid=42&img=D:\xampp\htdocs\joomla_2.5.3\images\phocagalle ry\01-barcelona.jpg";
$fileSize = strlen(file_get_contents($fileOriginal));

$mimeType = "image/jpeg";
$fileName = $fileNameToDownload;



// Clean the output buffer
ob_end_clean();

header("Cache-Control: public, must-revalidate");
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Description: File Transfer");
header("Expires: Sat, 30 Dec 1990 07:07:07 GMT");
header("Content-Type: " . (string)$mimeType);


header("Content-Length: ". (string)$fileSize);


header('Content-Disposition: attachment; filename="'.$fileName.'"');
header("Content-Transfer-Encoding: binary\n");

@readfile($fileOriginal);
exit;