这是我以前的PHP问题的后续行动。在ALFEaton帮助我完成代码之后,我进行了一些额外的更改以添加额外的功能。但是,我遇到了一个小问题,我无法弄明白。代码评论相当好,所以应该很容易阅读:
<?php
class Functions {
/**
* Allows you to save two copies of an image;
* the resized copy, and the actual copy
*
* @param $file - URL to get images from */
public function resize_image($file) {
// Gets the file name
$path_parts = pathinfo($file);
$filename_thumb = 'thumbs/' . $path_parts['filename'] . '.png';
$filename = 'gallery/' . $path_parts['filename'] . '.png';
if (!file_exists($filename_thumb)) {
//Creates an image from the URL
$image = imagecreatefrompng($file);
//Defining the desired thumbnail size, and the current one
$thumb_size = 150;
$width = imagesx($image);
$height = imagesy($image);
//Defining image aspect ratio
$original_aspect = $width / $height;
$thumb_aspect = $thumb_size / $thumb_size;
if ($original_aspect >= $thumb_aspect) {
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_size;
$new_width = $width / ($height / $thumb_size);
} else {
// If the thumbnail is wider than the image
$new_width = $thumb_size;
$new_height = $height / ($width / $thumb_size);
}
$thumb = imagecreatetruecolor($thumb_size, $thumb_size);
// Resize and crop
imagecopyresampled($thumb, $image, 0 - ($new_width - $thumb_size) / 2, // Center the image horizontally
0 - ($new_height - $thumb_size) / 2, // Center the image vertically
0, 0, $new_width, $new_height, $width, $height);
imagepng($thumb, $filename_thumb, 9);
} else {
if (!file_exists($filename)) {
$image_large = imagecreatefrompng($file);
imagepng($image_large, $filename, 9);
} else {
// Do Nothing
}
}
}
/**
* Adds required HTML nodes to index.php
*
* @author AlfEaton from Stack Overflow */
public function getImages() {
//Returns the index page
$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc -> loadHTMLFile('http://hathorarts.deviantart.com/gallery/');
libxml_use_internal_errors(false);
//Selects the linked thumbnail images
$xpath = new DOMXPath($doc);
$nodes = $xpath -> query('//a[@class="thumb"]');
// Get the URL for each actual image, and build the links
$output = new DOMDocument;
foreach ($nodes as $i => $node) {
$source_pre = $node -> getAttribute('data-super-img');
$source = str_replace("/PRE", "", $source_pre);
$this -> resize_image($source);
$path_parts = pathinfo($source);
$path_parts_filename = $path_parts['filename'];
$filename = 'thumbs/' . $path_parts_filename . '.png';
$filename_original = 'gallery/' . $path_parts_filename . '.png';
$image_thumb = $output -> createElement('img');
$image_thumb -> setAttribute('class', 'image_thumb');
$image_thumb -> setAttribute('src', $filename);
$link = $output -> createElement('a');
$link -> setAttribute('href', $filename_original);
$link -> setAttribute('rel', 'lightbox-gallery');
$link -> appendChild($image_thumb);
$output -> appendChild($link);
}
print $output -> saveHTML();
}
}
?>
缩略图部分正常。但$ link href属性返回缩略图。这是我为这个项目设置的文件 -
将我的PHP函数放在functions.php文件中。 代码输出实际图像和缩略图,但它不显示实际图像,只显示缩略图。
答案 0 :(得分:0)
好的,这可能听起来像一个愚蠢的问题,但你确定你正在用这些功能创建全尺寸图像吗?如果缩略图文件存在,则resize_image函数仅创建完整大小的图像 - 它不会在缩略图例程中创建它:
您的代码:
if (!file_exists($filename_thumb)) {
//Creates an image from the URL
$image = imagecreatefrompng($file);
//Defining the desired thumbnail size, and the current one
//*and carry on with your thumbnail creation*
} else {
if (!file_exists($filename)) {
$image_large = imagecreatefrompng($file);
imagepng($image_large, $filename, 9);
}
你应该放最后一行 - imagepng($ image_large,$ filename,9);在缩略图程序中也是吗?
建议:
if (!file_exists($filename_thumb)) {
//Creates an image from the URL
$image = imagecreatefrompng($file);
imagepng($image, $filename, 9);
//Defining the desired thumbnail size, and the current one
//*and carry on with your thumbnail creation*
} else {
if (!file_exists($filename)) {
$image_large = imagecreatefrompng($file);
imagepng($image_large, $filename, 9);
}