[英]PHP Image Resize (gallery)
I've recently built a website using PHP for the first time. 我最近第一次使用PHP建立了一个网站。 I have create a gallery script using various tutorials which automatically pulls through images held in a set location and creates a thumbnail, if one does not exit.
我使用各种教程创建了一个画廊脚本,这些脚本会自动浏览设置在某个位置的图像并创建缩略图(如果没有退出的话)。
Now my upload works fine as does pulling through the images, however the script for some reason, will not re-size the image! 现在,我的上传效果很好,可以浏览图像,但是由于某种原因,脚本不会调整图像大小! This is really beginning to baffle me now and as I have never used php before, I am at a total loss.
这真的开始使我感到困惑,因为我以前从未使用过php,所以我全无所获。 My script it below
我的脚本在下面
<?php
# SETTINGS
$max_width = 100;
$max_height = 100;
function getPictureType($ext) {
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function getPictures() {
global $max_width, $max_height;
if ( $handle = opendir("Design/Images/Gallery/") ) {
$lightbox = rand();
echo '<div id="gallery"><ul>';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) ) {
$split = explode('.', $file);
$ext = $split[count($split) - 1];
if ( ($type = getPictureType($ext)) == '' ) {
continue;
}
if ( ! is_dir('Design/Images/Gallery/thumbs') ) {
mkdir('Design/Images/Gallery/thumbs');
}
if ( ! file_exists('Design/Images/Gallery/thumbs/'.$file) ) {
if ( $type == 'jpg' ) {
$src = imagecreatefromjpeg($file);
} else if ( $type == 'png' ) {
$src = imagecreatefrompng($file);
} else if ( $type == 'gif' ) {
$src = imagecreatefromgif($file);
}
if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
$newW = $oldW * ($max_width / $oldH);
$newH = $max_height;
} else {
$newW = $max_width;
$newH = $oldH * ($max_height / $oldW);
}
$new = imagecreatetruecolor($newW, $newH);
imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
if ( $type == 'jpg' ) {
imagejpeg($new, 'Design/Images/Gallery/thumbs/'.$file);
} else if ( $type == 'png' ) {
imagepng($new, 'Design/Images/Gallery/thumbs/'.$file);
} else if ( $type == 'gif' ) {
imagegif($new, 'Design/Images/Gallery/thumbs/'.$file);
}
imagedestroy($new);
imagedestroy($src);
}
echo '<li><a href="Design/Images/Gallery/'.$file.'" rel="lightbox['.$lightbox.']">';
echo '<img src="Design/Images/Gallery/thumbs/'.$file.'" alt="" />';
echo '</a></li>';
}
}
echo '</ul></div>';
}
}
?> ?>
I believe the error is something to do with my paths but I am not sure, can anyone shed some light on this???? 我相信该错误与我的路径有关,但我不确定,有人可以对此有所了解吗????
//Simply pass the parameter in below function n u'll get the resized image. //只需在下面的函数中传递参数,您将获得调整大小的图像。
for Ex. 例如
$sourcefile = SITE_PATH."pro_images/".$main_img_name; $ sourcefile = SITE_PATH。“ pro_images /".$ main_img_name;
$thumb = "thumb_".$main_img_name;
$endfile = SITE_PATH."pro_images/thumb/".$thumb;
$thumbheight = 80;
$thumbwidth = 80;
$quality = 75;
function createThumbs($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality){
preg_match("'^(.*).(gif|jpe?g|png)$'i", $sourcefile, $ext); preg_match(“'^(。*)。(gif | jpe?g | png)$'i”,$ sourcefile,$ ext);
switch (strtolower($ext[2])) { 开关(strtolower($ ext [2])){
case 'jpg' :
case 'jpeg': $img = imagecreatefromjpeg ($sourcefile);
break;
case 'gif' : $img = imagecreatefromgif ($sourcefile);
break;
case 'png' : $img = imagecreatefrompng ($sourcefile); case'png':$ img = imagecreatefrompng($ sourcefile);
break; 打破;
} }
//$img = imagecreatefromjpeg($sourcefile); // $ img = imagecreatefromjpeg($ sourcefile);
$width = imagesx( $img ); $ width = imagesx($ img); $height = imagesy( $img );
$ height = imagesy($ img);
$scale = $thumbwidth/$width; $ scale = $ thumbwidth / $ width;
$newwidth = ceil($width * $scale);
$newheight = ceil($height * $scale);
// Create a new temporary image. //创建一个新的临时映像。
$tmpimg = imagecreatetruecolor( $newwidth, $newheight ); $ tmpimg = imagecreatetruecolor($ newwidth,$ newheight);
// Copy and resize old image into new image. //复制旧图像并将其调整为新图像。
imagecopyresampled( $tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height ); imagecopyresampled($ tmpimg,$ img,0,0,0,0,$ newwidth,$ newheight,$ width,$ height);
// Save thumbnail into a file. //将缩略图保存到文件中。
imagejpeg( $tmpimg, $endfile, $quality); imagejpeg($ tmpimg,$ endfile,$ quality);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.