我的PHP文件中有几个函数来处理图像上传
3调整大小的函数工作正常,但没有: watermark_image 。代码与其他resize-and-watermark函数的代码完全相同,但它只将SRC存储在我的数据库中,而不是目录中的图像文件本身。
我已经尝试更改所有变量名称(因为就像我说的,它们完全相同,但在不同的方法中),但这不起作用。
我在这里缺少什么?
要清楚,问题在于将新的已调整大小的图像保存在目录中,而不是在图像上放置水印。
resize_big函数与watermark_image函数完全相同,除了图像的大小。水印为135×38px。
形式:
<form action="" method="POST" enctype="multipart/form-data" style="background-color:white; margin-top:100px;">
<div>
<select name="category">
<option value="paard" selected>Paarden</option>
<option value="hond">Honden</option>
<option value="mens">Mensen</option>
<option value="eigen">Eigen werk</option>
</select><br/>
<input type="file" name="files[]" multiple id="file"/><Br/>
Model: <input type="text" name="model" id="model"/>
<br/>
<label class="radio">Portfoliomateriaal</label>
<input type="radio" name="folio" value="TRUE" /> <span>Ja</span>
<input type="radio" name="folio" value="FALSE" checked /> <span>Nee</span><p>
<input type="submit" value="Upload" id="submit" />
</div>
</form>
上传页面顶部的PHP功能:
<?php
error_reporting(E_ALL);
include "include/resize.php";
include "include/config.php";
if (isset($_GET['delete']))
{
// Delete hele directory: array_map('unlink', glob("some/dir/*.txt")); && Leeg tabel SQL: DELETE FROM table_name;or DELETE * FROM table_name; OF "TRUNCATE TABLE mytable;"
$deleteID = $_GET['delete'];
echo "<p style='color:white;'>".$deleteID."</p>";
$select = "SELECT * FROM photo WHERE id=".$deleteID;
$results = mysql_query($select) or die(mysql_error());
$row_count=mysql_num_rows($results);
while ($images = mysql_fetch_array($results)) {
unlink($images['orig']);
unlink($images['web']);
unlink($prev = $images['prev']);
unlink($thumb = $images['thumb']);
}
$deleteSQL = "DELETE FROM photo WHERE id=".$deleteID;
mysql_query($deleteSQL) or die(mysql_error());
}
$file_dest = "photos/orig/";
$thumb_dest = "photos/thumb/";
$web_dest = "photos/web/";
$prev_dest = "photos/preview/";
//$dir = 'uploads/foto/'.$_POST['cat'].'/';
if(!empty($_FILES['files']['name'][0])){
$files = $_FILES['files'];
$uploaded = array();
$bestanden = array();
$failed = array();
$allowedExt = array('png', 'jpg', 'gif');
foreach($files['name'] as $position => $file_name) {
$file_tmp = $files['tmp_name'][$position];
$file_size = $files['size'][$position];
$file_error = $files['error'][$position];
$file_cat = $_POST['category'];
$file_folio = $_POST['folio'];
$file_model = ucfirst($_POST['model']);
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
if(in_array($file_ext, $allowedExt)){
if($file_error == 0){
if($file_size <= 32000000){
$file_name_new = uniqid('', true)."_".$file_name;
$path_web = $web_dest.$file_name_new;
$path_thumb = $thumb_dest.$file_name_new;
$path_prev = $prev_dest.$file_name_new;
list($file_width, $file_height) = getimagesize($file_tmp);
//Wat is het grootste?
if($file_width > $file_height){
$largest = $file_width;
}else if($file_height > $file_width){
$largest = $file_height;
}else if($file_height = $file_width){
$largest = $file_height;
}
if($largest <= 800){
//echo "image is already web-sized";
$file_move = $web_dest.$file_name_new;
$path_orig = "";
}else{
$file_move = $file_dest.$file_name_new;
$path_orig = $file_move;
watermark_image($file_move, $path_web);
}
if(move_uploaded_file($file_tmp, $file_move)){
$uploaded[$position]['naam'] = $file_name;
$uploaded[$position]['pad'] = $file_move;
if($largest > 1080){
resize_big($file_move, $file_move);
}
resize_image($file_move, $path_prev);
create_thumbnail($file_move, $path_thumb, 75, 75);
$query = "INSERT INTO photo (name, cat, orig, web, prev, thumb, folio) VALUES ('$file_model', '$file_cat', '$path_orig', '$path_web', '$path_prev', '$path_thumb', '$file_folio')";
$result = mysql_query($query) or die(mysql_error());
//mysql_close();
}else{
$failed[$position] = "[{$file_name}] failed to upload.";
}
}else{
$failed[$position] = "[{$file_name}] is too large.";
}
}else{
$failed[$position] = "[{$file_name}] errored with code {$file_error}";
}
}else{
$failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed.";
}
}
if(!empty($uploaded)){
/*
echo "<pre>";
print_r($uploaded);
echo "</pre>";
*/
}
if(!empty($failed)){
print_r($failed);
}
header('location:'.$_SERVER['PHP_SELF']);die;
//end foreach function
}else{
echo "No files were added.";
}
?>
包含调整大小函数的PHP文件:
<?php
//WATERMERK and RESIZE function
//Watermark function
function watermark_image($file, $destination){
//Get watermark
$watermark = imagecreatefrompng('images/watermerk.png');
//Max width and height of resized image
$width = 800;
$height = 800;
//Type and size file
$source = getimagesize($file);
//Get part of the array, e.g. image/jpeg
$source_mime = $source['mime'];
//Check which file, then make image
if($source_mime == 'image/png'){
$image = imagecreatefrompng($file);
}else if($source_mime == 'image/jpeg'){
$image = imagecreatefromjpeg($file);
} else if($source_mime == 'image/gif'){
$image = imagecreatefromgif($file);
}
//New dimensions
list($file_width, $file_height) = getimagesize($file);
$file_ratio = $file_width/$file_height;
if ($width/$height > $file_ratio) {
$width = $height*$file_ratio;
} else {
$height = $width/$file_ratio;
}
// Resample
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $file_width, $file_height);
//Put size of picture in variable
$file_x = imagesx($new_image);
$file_y = imagesy($new_image);
//Size watermark
$logo_x = imagesx($watermark);
$logo_y = imagesy($watermark);
//Place of watermark
$pos_x = 1;
$pos_y = $file_y - $logo_y + 1;
//Put watermark on image
imagecopy($new_image, $watermark, $pos_x, $pos_y, 0, 0, $logo_x, $logo_y);
imagepng($new_image, $destination);
//echo $destination;
//Destroy temporary files
imagedestroy($new_image);
imagedestroy($image);
imagedestroy($watermark);
}
/*==============================================================================*/
//PREV RESIZE FUNCTIE
function resize_image($file, $destination){
//Get watermark
$watermark = imagecreatefrompng('images/copy.png');
//max. width and height of resized image.
$width = 400;
$height = 400;
//Type and size of file
$source = getimagesize($file);
//Get part of array, e.g. image/jpeg
$source_mime = $source['mime'];
//Check which file, then make image
if($source_mime == 'image/png'){
$image = imagecreatefrompng($file);
}else if($source_mime == 'image/jpeg'){
$image = imagecreatefromjpeg($file);
} else if($source_mime == 'image/gif'){
$image = imagecreatefromgif($file);
}
//Set new dimensions
list($file_width, $file_height) = getimagesize($file);
$file_ratio = $file_width/$file_height;
if ($width/$height > $file_ratio) {
$width = $height*$file_ratio;
} else {
$height = $width/$file_ratio;
}
// Resample
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $file_width, $file_height);
//Put size of image in variable
$file_x = imagesx($new_image);
$file_y = imagesy($new_image);
//Size of watermark
$logo_x = imagesx($watermark);
$logo_y = imagesy($watermark);
//Place of watermark
$pos_x = ($file_x - $logo_x)/2;
$pos_y = ($file_y - $logo_y)/2;
//Put watermark on image
imagecopy($new_image, $watermark, $pos_x, $pos_y, 0, 0, $logo_x, $logo_y);
imagepng($new_image, $destination);
//echo $destination;
//Destroy temporary files
imagedestroy($new_image);
imagedestroy($image);
imagedestroy($watermark);
}
/*==============================================================================*/
//BIG RESIZE FUNCTION
function resize_big($file, $destination){
//Watermark ophalen in php
$watermark = imagecreatefrompng('images/watermerk.png');
//De maximum breedte en hoogte van de geresizede foto.
$width = 1920;
$height = 1080;
//Type en grootte van het bestand ophalen
$source = getimagesize($file);
//Gedeelte van het array ophalen, bijv image/jpeg
$source_mime = $source['mime'];
//Controleren welk type het bestand is, dan hiervoor een plaatje aanmaken
if($source_mime == 'image/png'){
$image = imagecreatefrompng($file);
}else if($source_mime == 'image/jpeg'){
$image = imagecreatefromjpeg($file);
} else if($source_mime == 'image/gif'){
$image = imagecreatefromgif($file);
}
//Nieuwe afmetingen bepalen
list($file_width, $file_height) = getimagesize($file);
$file_ratio = $file_width/$file_height;
if ($width/$height > $file_ratio) {
$width = $height*$file_ratio;
} else {
$height = $width/$file_ratio;
}
// Resample
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $file_width, $file_height);
//Grootte van de foto in een variabele stoppen
$file_x = imagesx($new_image);
$file_y = imagesy($new_image);
//Grootte van het watermerk bepalen
$logo_x = imagesx($watermark);
$logo_y = imagesy($watermark);
//Plaats van het watermerk bepalen
$pos_x = 1;
$pos_y = $file_y - $logo_y + 1;
//Watermerk erop plaatsen.
//Eerst het bronbestand, dan het watermerk, dan de positie (1p px van de top, 10 px van links), dan waar het bronbestand begint, dan de grootte van het watermerk definieren.
imagecopy($new_image, $watermark, $pos_x, $pos_y, 0, 0, $logo_x, $logo_y);
imagepng($new_image, $destination);
//echo $destination;
//Tijdelijke bestanden vernietigen
imagedestroy($new_image);
imagedestroy($image);
imagedestroy($watermark);
}
/*==============================================================================*/
//THUMBNAIL FUNCTION
function create_thumbnail($path, $save, $width, $height){
$info = getimagesize($path);
$size = array($info[0], $info[1]);
//Controleren welk type bestand het is
if($info['mime'] == 'image/png'){
$src = imagecreatefrompng($path);
}else if($info['mime'] == 'image/jpeg'){
$src = imagecreatefromjpeg($path);
}else if($info['mime'] == 'image/gif'){
$src = imagecreatefromgif($path);
}else{
return false;
}
//Thumbnail creeeren
$thumb = imagecreatetruecolor($width, $height);
//Aspect ratio's
$src_aspect = $size[0] / $size[1];
$thumb_aspect = $width / $height;
//Controleren of de foto horizontaal of verticaal is
if($src_aspect < $thumb_aspect){
//Verticaal, dus boven en onder afcroppen
//resize
$scale = $width / $size[0];
$new_size = array($width, $width / $src_aspect);
//Croppen
$src_pos = array(0, ($size[1] * $scale - $height) / $scale / 2);
}else if($src_aspect > $thumb_aspect){
//Horizontaal, dus links en rechts afcroppen
//resize
$scale = $height / $size[1];
$new_size = array($height * $src_aspect, $height);
//croppen
$src_pos = array(($size[0] * $scale - $width) / $scale / 2, 0);
}else{
//Vierkant
//resize
$new_size = array($width, $height);
//croppen
$src_pos = array(0, 0);
}
//fouten opvangen
$new_size[0] = max($new_size[0], 1);
$new_size[1] = max($new_size[1], 1);
//Geresizede foto op de thumbnail plakken.
//Gemaakte thumbnail, originele plaatje, begint linksbovenin, locatie waar je van wil kopieren x y, nieuwe grootte van de foto (Geresized) x y, originele grootte van de foto
imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $size[0], $size[1]);
if($save == false){
return imagepng($thumb);
}else{
//Saven
return imagepng($thumb, $save);
}
}
?>