Php 如何在目录中获取原始图像创建的缩略图的文件路径
我正在使用一个函数来存储存储在目录中的原始图像的缩略图,现在我需要将该缩略图的文件路径保存在db中 我怎样才能得到我的缩略图的路径Php 如何在目录中获取原始图像创建的缩略图的文件路径,php,mysql,Php,Mysql,我正在使用一个函数来存储存储在目录中的原始图像的缩略图,现在我需要将该缩略图的文件路径保存在db中 我怎样才能得到我的缩略图的路径 <?php ob_start(); ?> <?php function make_thumb($src, $dest, $desired_width) { /* read the source image */ $source_image = imagecreatefromjpeg($src);
<?php ob_start(); ?>
<?php
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
echo $thumb ."this is result";
$sql="INSERT INTO images thumb VALUES('$thumb')";
$result = mysql_query($sql) or die(mysql_error());
}
?>
<?php
//This is the directory where images will be saved
$target = "uploads/images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$pic=($_FILES['photo']['name']);
// Connects to your Database
include('connect.php');
//Writes the information to the database
$sql="INSERT INTO images (name, alt)
VALUES('$pic','$_POST[altText]')";
$result = mysql_query($sql) or die(mysql_error());
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
<?php
$src=$target;
$dest="uploads/thumbs/". basename( $_FILES[photo][name]);
$desired_width=160;
$quality=80;
make_thumb($src, $dest, $desired_width,$quality);
// here i need the code to save the file path of thumbnail
?>
<?php
//unset($_POST);
// 5. Close connection
mysql_close($connection);
header("Location: images.php");
?>
<?php ob_flush(); ?>
$dest
是您的缩略图图像路径。您已将其传递给make_thumb()
函数
$dest="uploads/thumbs/". basename( $_FILES[photo][name]);
function imgOptCpy($inFile, $outFile="", $width=200, $height=200, $quality=100, $resize=false)
{
//----------------------------------------
//echo "tanmoy-".$inFile;
//exit;
list($width_original, $height_original) = @getimagesize($inFile);
if($resize)
{
if($width)
{
$height2 = ($width / $width_original) * $height_original;
if($height && $height2 > $height)
$width = ($height / $height_original) * $width_original;
else
$height = $height2;
//echo 'width = '.$width;
//echo '- height = '.$height;
}
elseif($height)
{
$width = ($height / $height_original) * $width_original;
//echo 'width = '.$width;
//echo '- height = '.$height;
}
else
{
list($width, $height) = @getimagesize($inFile);
//echo 'width = '.$width;
//echo '- height = '.$height;
}
}
//echo 'width = '.$width;
//echo '- height = '.$height;
//----------------------------------------
$inType = @strtolower(@strrev(@substr(@strrev($inFile), 0, 4)));
if(!$outFile)
$outFile = $inFile;
$outType = @strtolower(@strrev(@substr(@strrev($outFile), 0, 4)));
$image_xy = @imagecreatetruecolor($width, $height);
//----------------------------------------
if($inType==".jpg" || $inType=="jpeg")
{
//echo $inFile ; //exit ;
$image = @imagecreatefromjpeg($inFile);
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
elseif($inType==".gif")
{
$image = @imagecreatefromgif($inFile);
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
elseif($inType==".png")
{
//echo $inFile ; //exit ;
$image = @imagecreatefrompng($inFile) ;
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
//----------------------------------------
if($outType==".jpg" || $outType=="jpeg")
{
//@header("Content-Type: image/jpeg");
$ok = @imagejpeg($image_xy, $outFile, $quality);
}
elseif($outType==".gif")
{
//@header("Content-Type: image/gif");
//$ok = @imagegif($image_xy, $outFile, $quality);
$ok = @imagegif($image_xy, $outFile);
}
elseif($outType==".png")
{
//@header("Content-Type: image/png");
if ($quality == 100) $quality = 9;
$ok = @imagepng($image_xy, $outFile, $quality);
}
@chmod($outFile, 0777);
//echo "gfg-".$inFile;
// exit;
//----------------------------------------
}
成功创建缩略图后,应通过make_thumb()
函数返回true
$thumbFlag = make_thumb($src, $dest, $desired_width,$quality);
if($thumbFlag){
// write your insert query here to save thumb path (e.g, $dest)
} else {
// handle error here
}
$dest
是您的缩略图图像路径。您已将其传递给make_thumb()
函数
$dest="uploads/thumbs/". basename( $_FILES[photo][name]);
function imgOptCpy($inFile, $outFile="", $width=200, $height=200, $quality=100, $resize=false)
{
//----------------------------------------
//echo "tanmoy-".$inFile;
//exit;
list($width_original, $height_original) = @getimagesize($inFile);
if($resize)
{
if($width)
{
$height2 = ($width / $width_original) * $height_original;
if($height && $height2 > $height)
$width = ($height / $height_original) * $width_original;
else
$height = $height2;
//echo 'width = '.$width;
//echo '- height = '.$height;
}
elseif($height)
{
$width = ($height / $height_original) * $width_original;
//echo 'width = '.$width;
//echo '- height = '.$height;
}
else
{
list($width, $height) = @getimagesize($inFile);
//echo 'width = '.$width;
//echo '- height = '.$height;
}
}
//echo 'width = '.$width;
//echo '- height = '.$height;
//----------------------------------------
$inType = @strtolower(@strrev(@substr(@strrev($inFile), 0, 4)));
if(!$outFile)
$outFile = $inFile;
$outType = @strtolower(@strrev(@substr(@strrev($outFile), 0, 4)));
$image_xy = @imagecreatetruecolor($width, $height);
//----------------------------------------
if($inType==".jpg" || $inType=="jpeg")
{
//echo $inFile ; //exit ;
$image = @imagecreatefromjpeg($inFile);
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
elseif($inType==".gif")
{
$image = @imagecreatefromgif($inFile);
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
elseif($inType==".png")
{
//echo $inFile ; //exit ;
$image = @imagecreatefrompng($inFile) ;
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
//----------------------------------------
if($outType==".jpg" || $outType=="jpeg")
{
//@header("Content-Type: image/jpeg");
$ok = @imagejpeg($image_xy, $outFile, $quality);
}
elseif($outType==".gif")
{
//@header("Content-Type: image/gif");
//$ok = @imagegif($image_xy, $outFile, $quality);
$ok = @imagegif($image_xy, $outFile);
}
elseif($outType==".png")
{
//@header("Content-Type: image/png");
if ($quality == 100) $quality = 9;
$ok = @imagepng($image_xy, $outFile, $quality);
}
@chmod($outFile, 0777);
//echo "gfg-".$inFile;
// exit;
//----------------------------------------
}
成功创建缩略图后,应通过make_thumb()
函数返回true
$thumbFlag = make_thumb($src, $dest, $desired_width,$quality);
if($thumbFlag){
// write your insert query here to save thumb path (e.g, $dest)
} else {
// handle error here
}
使用此功能
$dest="uploads/thumbs/". basename( $_FILES[photo][name]);
function imgOptCpy($inFile, $outFile="", $width=200, $height=200, $quality=100, $resize=false)
{
//----------------------------------------
//echo "tanmoy-".$inFile;
//exit;
list($width_original, $height_original) = @getimagesize($inFile);
if($resize)
{
if($width)
{
$height2 = ($width / $width_original) * $height_original;
if($height && $height2 > $height)
$width = ($height / $height_original) * $width_original;
else
$height = $height2;
//echo 'width = '.$width;
//echo '- height = '.$height;
}
elseif($height)
{
$width = ($height / $height_original) * $width_original;
//echo 'width = '.$width;
//echo '- height = '.$height;
}
else
{
list($width, $height) = @getimagesize($inFile);
//echo 'width = '.$width;
//echo '- height = '.$height;
}
}
//echo 'width = '.$width;
//echo '- height = '.$height;
//----------------------------------------
$inType = @strtolower(@strrev(@substr(@strrev($inFile), 0, 4)));
if(!$outFile)
$outFile = $inFile;
$outType = @strtolower(@strrev(@substr(@strrev($outFile), 0, 4)));
$image_xy = @imagecreatetruecolor($width, $height);
//----------------------------------------
if($inType==".jpg" || $inType=="jpeg")
{
//echo $inFile ; //exit ;
$image = @imagecreatefromjpeg($inFile);
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
elseif($inType==".gif")
{
$image = @imagecreatefromgif($inFile);
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
elseif($inType==".png")
{
//echo $inFile ; //exit ;
$image = @imagecreatefrompng($inFile) ;
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
//----------------------------------------
if($outType==".jpg" || $outType=="jpeg")
{
//@header("Content-Type: image/jpeg");
$ok = @imagejpeg($image_xy, $outFile, $quality);
}
elseif($outType==".gif")
{
//@header("Content-Type: image/gif");
//$ok = @imagegif($image_xy, $outFile, $quality);
$ok = @imagegif($image_xy, $outFile);
}
elseif($outType==".png")
{
//@header("Content-Type: image/png");
if ($quality == 100) $quality = 9;
$ok = @imagepng($image_xy, $outFile, $quality);
}
@chmod($outFile, 0777);
//echo "gfg-".$inFile;
// exit;
//----------------------------------------
}
并在需要绘制缩略图的地方调用它
imgOptCpy($upload_target_original, $upload_path_for_thumbnail, 270, 0, 215, true);
并将$upload\u path\u保存到数据库中的缩略图中。使用此功能
$dest="uploads/thumbs/". basename( $_FILES[photo][name]);
function imgOptCpy($inFile, $outFile="", $width=200, $height=200, $quality=100, $resize=false)
{
//----------------------------------------
//echo "tanmoy-".$inFile;
//exit;
list($width_original, $height_original) = @getimagesize($inFile);
if($resize)
{
if($width)
{
$height2 = ($width / $width_original) * $height_original;
if($height && $height2 > $height)
$width = ($height / $height_original) * $width_original;
else
$height = $height2;
//echo 'width = '.$width;
//echo '- height = '.$height;
}
elseif($height)
{
$width = ($height / $height_original) * $width_original;
//echo 'width = '.$width;
//echo '- height = '.$height;
}
else
{
list($width, $height) = @getimagesize($inFile);
//echo 'width = '.$width;
//echo '- height = '.$height;
}
}
//echo 'width = '.$width;
//echo '- height = '.$height;
//----------------------------------------
$inType = @strtolower(@strrev(@substr(@strrev($inFile), 0, 4)));
if(!$outFile)
$outFile = $inFile;
$outType = @strtolower(@strrev(@substr(@strrev($outFile), 0, 4)));
$image_xy = @imagecreatetruecolor($width, $height);
//----------------------------------------
if($inType==".jpg" || $inType=="jpeg")
{
//echo $inFile ; //exit ;
$image = @imagecreatefromjpeg($inFile);
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
elseif($inType==".gif")
{
$image = @imagecreatefromgif($inFile);
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
elseif($inType==".png")
{
//echo $inFile ; //exit ;
$image = @imagecreatefrompng($inFile) ;
@imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
$width, $height, $width_original, $height_original);
}
//----------------------------------------
if($outType==".jpg" || $outType=="jpeg")
{
//@header("Content-Type: image/jpeg");
$ok = @imagejpeg($image_xy, $outFile, $quality);
}
elseif($outType==".gif")
{
//@header("Content-Type: image/gif");
//$ok = @imagegif($image_xy, $outFile, $quality);
$ok = @imagegif($image_xy, $outFile);
}
elseif($outType==".png")
{
//@header("Content-Type: image/png");
if ($quality == 100) $quality = 9;
$ok = @imagepng($image_xy, $outFile, $quality);
}
@chmod($outFile, 0777);
//echo "gfg-".$inFile;
// exit;
//----------------------------------------
}
并在需要绘制缩略图的地方调用它
imgOptCpy($upload_target_original, $upload_path_for_thumbnail, 270, 0, 215, true);
并将$upload\u path\u保存到数据库中的缩略图中。这就是我所做的更改,正如Roopendra所提到的
<?php
$src=$target;
$dest="uploads/thumbs/". basename( $_FILES['photo']['name']);
$desired_width=160;
$quality=80;
// make_thumb($src, $dest, $desired_width,$quality);
$thumbFlag = make_thumb($src, $dest, $desired_width,$quality);
echo $dest;
if(!$thumbFlag){
// write your insert query here to save thumb path (e.g, $dest)
$sql = "UPDATE images SET thumb='$dest' WHERE name='$pic'";
mysql_query($sql) or die(mysql_error());
}
else {
// handle error here
echo "Sorry unable to store the image";
}
?>
这就是我做了Roopendra提到的更改的地方
<?php
$src=$target;
$dest="uploads/thumbs/". basename( $_FILES['photo']['name']);
$desired_width=160;
$quality=80;
// make_thumb($src, $dest, $desired_width,$quality);
$thumbFlag = make_thumb($src, $dest, $desired_width,$quality);
echo $dest;
if(!$thumbFlag){
// write your insert query here to save thumb path (e.g, $dest)
$sql = "UPDATE images SET thumb='$dest' WHERE name='$pic'";
mysql_query($sql) or die(mysql_error());
}
else {
// handle error here
echo "Sorry unable to store the image";
}
?>
如何在make_thumb()
函数中获得$thumb
功能?哦,很抱歉,我尝试了一个函数来存储缩略图的路径以存储在$thumb中,并通过注释标题来回显它,但没有起作用。如何在make_thumb()
函数中获得$thumb
功能?哦,很抱歉,我尝试了一个函数来存储缩略图的路径,以存储在$thumb中,并通过注释标题来响应它,但没有起作用。