Php 为所有mysql post img附件创建拇指失败
我正在尝试保存我网站中所有附加图像的副本 加载时间太长,所以我认为生成要在列表页面中显示的缩略图不是个坏主意 这就是我尝试的方式:Php 为所有mysql post img附件创建拇指失败,php,image,resize,thumbnails,Php,Image,Resize,Thumbnails,我正在尝试保存我网站中所有附加图像的副本 加载时间太长,所以我认为生成要在列表页面中显示的缩略图不是个坏主意 这就是我尝试的方式: <?php include('basedatos.php'); class ImgResizer { var $originalFile = '$newName'; function ImgResizer($originalFile = '$newName') { $this ->
<?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);
}
}
?>
根据您的代码重写您的类:
需要一个文件名作为其参数,您正在传入一个图像资源。是否尝试过?我在工作中使用它是为了获得类似的结果,它工作得非常好嗨,“imagedestroy($temp)”不会破坏原始图像吗?嘿,我用PNG文件的错误编辑了我的问题,你知道吗?它生成白色图像..PNG的质量是0到10的值,因此将95更改为0.95解决了此问题;)请参阅:了解png错误或使用
imagepng($tmp,$targetFile,9)代码><代码>图像PNG
将图像写入文件<代码>图像销毁清除内存。
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
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');