?? image_processing.php
字號:
<?# Image processing functions# Functions to allow upload and resizing of imagesif (!function_exists("upload_file")){function upload_file($ref) { # Process file upload for resource $ref # Work out which file has been posted (switch is necessary for SWFUpload) if (isset($_FILES['userfile'])) {$processfile=$_FILES['userfile'];} else {$processfile=$_FILES['Filedata'];} $filename=strtolower(str_replace(" ","_",$processfile['name'])); # Work out extension $extension=explode(".",$filename);$extension=trim(strtolower($extension[count($extension)-1])); $status="Please provide a file name."; $filepath=get_resource_path($ref,"",true,$extension); if ($filename!="") { $result=move_uploaded_file($processfile['tmp_name'], $filepath); if ($result==false) { $status="File upload error. Please check the size of the file you are trying to upload."; } else { chmod($filepath,0777); $status="Your file has been uploaded."; } } # Store extension in the database sql_query("update resource set file_extension='$extension',preview_extension='$extension' where ref='$ref'"); # Store original filename in field, if set global $filename_field; if (isset($filename_field)) { update_field($ref,$filename_field,$filename); } # Clear any existing FLV file or multi-page previews. global $pdf_pages; for ($n=2;$n<=$pdf_pages;$n++) { # Remove preview page. $path=get_resource_path($ref,"scr",false,"jpg",-1,$n,false); if (file_exists($path)) {unlink($path);} # Also try the watermarked version. $path=get_resource_path($ref,"scr",false,"jpg",-1,$n,true); if (file_exists($path)) {unlink($path);} } # Remove any FLV video preview (except if the actual resource is an FLV file). if ($extension!="flv") { $path=get_resource_path($ref,"",false,"flv"); if (file_exists($path)) {unlink($path);} } # Create previews create_previews($ref,false,$extension); return $status; }} function extract_exif_comment($ref,$extension) { # Extract the EXIF comment from either the ImageDescription field or the UserComment # Also parse IPTC headers and insert # EXIF headers $image=get_resource_path($ref,"",false,$extension); if (!file_exists($image)) {return false;}global $exiftool_path;if (isset($exiftool_path)) { if (file_exists(stripslashes($exiftool_path) . "/exiftool")) { #creating the exiftool command which will output the metadata array $command=$exiftool_path."/exiftool -p ' ("; $read_from=get_exiftool_fields(); for($i=0;$i< count($read_from);$i++) { $field=explode(",",$read_from[$i]['exiftool_field']); foreach ($field as $field){ $command.="\"$".$field."\", " ;} } #-f and -m force empty output ("-") and avoid error messages #-ScanforXMP allows tags to be extracted for unrecognized filetypes (INDD) #-fast avoids ScanforXMP if the filetype is a known one $command.=")' -f -m -ScanforXMP -fast $image"; $metadata=shell_exec($command); if(isset($metadata)){ #the printed output is evaluated into a php array eval('$'.'metadata_array=array'.$metadata.';'); #this is a rather complex double loop to account for the one-to-many relationship #j increases as all the read_from values are exhausted, so it allows #us to use all the values in the metadata array $j=0; for($i=0;$i< count($read_from);$i++) { $field=explode(",",$read_from[$i]['exiftool_field']); foreach ($field as $field){ #notice if two different values get mapped to the same field, the last one will be the winner #but if a previous field has a value and a subsequent field is empty, #the one with the value should get entered into the database. #files that have come from RS will have the same value for each anyway. if (($metadata_array[$j]!="-") && ($metadata_array[$j]!="")){update_field($ref,$read_from[$i]['ref'],$metadata_array[$j]);} $j++;} } } } }else{ $data=@exif_read_data($image); if ($data!==false) { $comment=""; #echo "<pre>EXIF\n";print_r($data);exit(); if (isset($data["ImageDescription"])) {$comment=$data["ImageDescription"];} if (($comment=="") && (isset($data["COMPUTED"]["UserComment"]))) {$comment=$data["COMPUTED"]["UserComment"];} if ($comment!="") { # Convert to UTF-8 $comment=iptc_return_utf8($comment); # Save comment global $exif_comment; update_field($ref,$exif_comment,$comment); } if (isset($data["Model"])) { # Save camera make/model global $exif_model; update_field($ref,$exif_model,$data["Model"]); } if (isset($data["DateTimeOriginal"])) { # Save camera date/time global $exif_date; $date=$data["DateTimeOriginal"]; # Reformat date to ISO standard $date=substr($date,0,4) . "-" . substr($date,5,2) . "-" . substr($date,8); update_field($ref,$exif_date,$date); } } # Try IPTC headers $size = getimagesize($image, $info); if (isset($info["APP13"])) { $iptc = iptcparse($info["APP13"]); #echo "<pre>IPTC\n";print_r($iptc);exit(); # Look for iptc fields, and insert. $fields=sql_query("select * from resource_type_field where length(iptc_equiv)>0"); for ($n=0;$n<count($fields);$n++) { $iptc_equiv=$fields[$n]["iptc_equiv"]; if (isset($iptc[$iptc_equiv][0])) { # Found the field if (count($iptc[$iptc_equiv])>1) { # Multiple values (keywords) $value=""; for ($m=0;$m<count($iptc[$iptc_equiv]);$m++) { if ($m>0) {$value.=", ";} $value.=$iptc[$iptc_equiv][$m]; } } else { $value=$iptc[$iptc_equiv][0]; } $value=iptc_return_utf8($value); # Date parsing if ($fields[$n]["type"]==4) { $value=substr($value,0,4) . "-" . substr($value,4,2) . "-" . substr($value,6,2); } if (trim($value)!="") {update_field($ref,$fields[$n]["ref"],$value);} } } } } }function iptc_return_utf8($text) { # For the given $text, return the utf-8 equiv. # Used for iptc headers to auto-detect the character encoding. global $iptc_expectedchars; # No inconv library? Return text as-is if (!function_exists("iconv")) {return $text;} $try=array("UTF-8","ISO-8859-1","Macintosh","Windows-1252"); for ($n=0;$n<count($try);$n++) { if ($try[$n]=="UTF-8") {$trans=$text;} else {$trans=@iconv($try[$n], "UTF-8", $text);} for ($m=0;$m<strlen($iptc_expectedchars);$m++) { if (strpos($trans,substr($iptc_expectedchars,$m,1))!==false) {return $trans;} } } return $text; } function create_previews($ref,$thumbonly=false,$extension="jpg") { # Always create file checksum (all types) generate_file_checksum($ref,$extension); if (($extension=="jpg") || ($extension=="jpeg") || ($extension=="png") || ($extension=="gif")) # Create image previews for built-in supported file types only (JPEG, PNG, GIF) { # For resource $ref, (re)create the various preview sizes listed in the table preview_sizes # Only create previews where the target size IS LESS THAN OR EQUAL TO the source size. # Set thumbonly=true to (re)generate thumbnails only. $file=get_resource_path($ref,"",false,$extension); # fetch source image size, if we fail, exit this function (file not an image, or file not a valid jpg/png/gif). if ((list($sw,$sh) = @getimagesize($file))===false) {return false;} $ps=sql_query("select * from preview_size" . (($thumbonly)?" where id='thm' or id='col'":"")); for ($n=0;$n<count($ps);$n++) { # fetch target width and height $tw=$ps[$n]["width"];$th=$ps[$n]["height"]; $id=$ps[$n]["id"]; # Find the target path and delete anything that's already there. $path=get_resource_path($ref,$ps[$n]["id"],false); if (file_exists($path)) {unlink($path);} # Also try the watermarked version. $wpath=get_resource_path($ref,$ps[$n]["id"],false,"jpg",-1,1,true); if (file_exists($wpath)) {unlink($wpath);} # only create previews where the target size IS LESS THAN OR EQUAL TO the source size. # or when producing a small thumbnail (to make sure we have that as a minimum if (($sw>$tw) || ($sh>$th) || ($id=="thm") || ($id=="col")) { # Calculate width and height. if ($sw>$sh) {$ratio = ($tw / $sw);} # Landscape else {$ratio = ($th / $sh);} # Portrait $tw=floor($sw*$ratio); $th=floor($sh*$ratio); global $imagemagick_path,$imagemagick_preserve_profiles,$imagemagick_quality; if (isset($imagemagick_path)) { # ---------------------------------------- # Use ImageMagick to perform the resize # ---------------------------------------- # Locate imagemagick. $command=$imagemagick_path . "/bin/convert"; if (!file_exists($command)) {$command=$imagemagick_path . "/convert";} if (!file_exists($command)) {$command=$imagemagick_path . "\convert.exe";} if (!file_exists($command)) {exit("Could not find ImageMagick 'convert' utility.'");} # Preserve colour profiles? (omit for smaller sizes) $profile="+profile icc +profile xmp +profile exif +profile iptc -colorspace RGB"; # By default, strip the colour profiles ('+' is remove the profile, confusingly) if ($imagemagick_preserve_profiles && $id!="thm" && $id!="col" && $id!="pre" && $id!="scr") {$profile="";} $command2 = $command . " \"$file\"[0] $profile -quality $imagemagick_quality -resize " . $tw . "x" . $th . " \"$path\""; $output=shell_exec($command2); if ($id=="thm") { # For the thumbnail image, call extract_mean_colour() to save the colour/size information $target=@imagecreatefromjpeg($path); extract_mean_colour($target,$ref); } # Add a watermarked image too? global $watermark; if (isset($watermark) && ($ps[$n]["internal"]==1 || $ps[$n]["allow_preview"]==1)) { $path=myrealpath(get_resource_path($ref,$ps[$n]["id"],false,"",-1,1,true)); if (file_exists($path)) {unlink($path);} $watermarkreal=myrealpath($watermark); $command2 = $command . " \"$file\"[0] $profile -quality $imagemagick_quality -resize " . $tw . "x" . $th . " -tile $watermarkreal -draw \"rectangle 0,0 $tw,$th\" \"$path\""; $output=shell_exec($command2); } } else { # ---------------------------------------- # Use the GD library to perform the resize # ---------------------------------------- $target = imagecreatetruecolor($tw,$th); if ($extension=="png") { $source = @imagecreatefrompng($file); if ($source===false) {return false;} } elseif ($extension=="gif") { $source = @imagecreatefromgif($file); if ($source===false) {return false;} } else {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -