我正在尝试在我的网站中保存所有附加图像的副本。
加载时间太大所以我认为生成缩略图以显示在列表页面中并不是一个坏主意,
这就是我的尝试:
<?php
include('basedatos.php');
class ImgResizer {
var $originalFile = '$newName';
function ImgResizer($originalFile = '$newName') {
$this -> originalFile = $originalFile;
}
function resize($newWidth, $targetFile) {
if (empty($newWidth) || empty($targetFile)) {
return false;
}
$src = imagecreatefromjpeg($this -> originalFile);
list($width, $height) = getimagesize($this -> originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (exif_imagetype($tmp) != IMAGETYPE_JPEG) {
imagecreatefromjpeg($tmp, $targetFile, 95);
}else if(exif_imagetype($tmp) != IMAGETYPE_PNG){
imagecreatefrompng($tmp, $targetFile, 95);
}else if(exif_imagetype($tmp) != IMAGETYPE_GIF){
imagecreatefromgif($tmp, $targetFile, 95);
}else die('invalid image format');
}
}
$query = "SELECT * FROM media where iframe <> 1";
$mediafiles = mysql_query($query);
while($details = mysql_fetch_array($mediafiles))
{ // Copy each file from its temp directory to $ROOT
$temp = '/home/deia1/public_html/'.$details['url'];
$path = '/home/deia1/public_html/files/uploads/thumbs/'.str_replace('files/uploads/','',$details['url']);
echo "$temp<br>";
if(is_dir('/home/deia1/public_html/files/uploads/thumbs/')){
echo "$path<br>";
$work = new ImgResizer($temp);
$work -> resize(300, $path);
}
}
?>
通知我有两个回声,
这就是他们记录的内容(对于第一项,其余的错误看起来几乎相同):
/home/deia1/public_html/files/uploads/Detalle-Magrana.jpg
/home/deia1/public_html/files/uploads/thumbs/Detalle-Magrana.jpg
警告:imagecreatefrompng()中的参数计数错误 第21行/home/deia1/public_html/includes/thumbs.php /home/deia1/public_html/files/uploads/video.png /home/deia1/public_html/files/uploads/thumbs/video.png
警告:imagecreatefromjpeg()[function.imagecreatefromjpeg]: gd-jpeg:JPEG库报告不可恢复的错误:in 第13行/home/deia1/public_html/includes/thumbs.php
警告:imagecreatefromjpeg()[function.imagecreatefromjpeg]: '/home/deia1/public_html/files/uploads/video.png'不是有效的JPEG 在第13行的/home/deia1/public_html/includes/thumbs.php中提交
知道我做错了什么吗?
- 编辑 -
我需要在/ thumbs / path中保存图像的已调整大小的副本,我不想调整原始图像的大小。
-EDIT2 -
这是我使用@Bass Jobsen为PNG文件建议的当前代码所产生的错误,并且它们生成白色图像。对于jpg似乎工作得很好:
Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib failed to initialize compressor -- stream error in /home/deia1/public_html/includes/thumbs.php on line 60
Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /home/deia1/public_html/includes/thumbs.php on line 60
答案 0 :(得分:2)
根据您的代码重写您的课程:
class ImgResizer {
var $originalFile = '$newName';
function ImgResizer($originalFile = '$newName') {
$this -> originalFile = $originalFile;
}
function resize($newWidth, $targetFile,$quality=90) {
if (empty($newWidth) || empty($targetFile)) {
return false;
}
if (($imgtype = exif_imagetype($this -> originalFile)) === IMAGETYPE_JPEG) {
$src = imagecreatefromjpeg($this -> originalFile);
}else if(($imgtype = exif_imagetype($this -> originalFile)) === IMAGETYPE_PNG){
$src = imagecreatefrompng($this -> originalFile);
}else if(($imgtype = exif_imagetype($this -> originalFile)) != IMAGETYPE_GIF){
$src = imagecreatefromgif($this -> originalFile);
}else die('invalid image format');
list($width, $height) = getimagesize($this -> originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagedestroy($src);
if ($imgtype === IMAGETYPE_JPEG) {
imagejpeg($tmp, $targetFile, $quality);
}else if($imgtype === IMAGETYPE_PNG){
//see: http://stackoverflow.com/a/7878801/1596547
imagepng($tmp, $targetFile, (int)$quality*9/100);
}else if($imgtype === IMAGETYPE_GIF){
imagegif($tmp, $targetFile);
}
imagedestroy($tmp);
}
}
$work = new ImgResizer('./testdrive/bootstrap-150x150.png');
$work -> resize(30, './thumb.png');