Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [Mod] Asynchronous image upload with resize new article (1.4)  (Read 2619 times)

Fotrane

  • Newbie
  • *
  • Karma: 1
  • Posts: 28
[Mod] Asynchronous image upload with resize new article (1.4)
« on: September 21, 2006, 07:26:25 pm »

I was getting tired of posting images *after* the article, so I tthrew this together. Feel free to rip it apart... :) Let me know if it does not work for you so I can see if I've missed anything.

Styles for the iframe (I just added them to my default stylesheet):
Code: [Select]
snews.php:
Find the new_article() function, and at or near line 1176 look for the following:
Code: [Select]
I replaced the



Create a new file: upload.php

Code: [Select]
require_once('snews.php');
function resize_jpeg( $image_file_path, $new_image_file_path, $max_width=340, $max_height=340 )
{
if ($_FILES['image']['tmp_name']){
$return_val = 1;

$return_val = ( ($img = ImageCreateFromJPEG ( $image_file_path )) && $return_val == 1 ) ? "1" : "0";

$FullImage_width = imagesx ($img); // Original image width
$FullImage_height = imagesy ($img); // Original image height

// now we check for over-sized images and pare them down
// to the dimensions we need for display purposes
$ratio =  ( $FullImage_width > $max_width ) ? (real)($max_width / $FullImage_width) : 1 ;
$new_width = ((int)($FullImage_width * $ratio)); //full-size width
$new_height = ((int)($FullImage_height * $ratio)); //full-size height
//check for images that are still too high
$ratio =  ( $new_height > $max_height ) ? (real)($max_height / $new_height) : 1 ;
$new_width = ((int)($new_width * $ratio)); //mid-size width
$new_height = ((int)($new_height * $ratio)); //mid-size height

// --Start Full Creation, Copying--
// now, before we get silly and 'resize' an image that doesn't need it...
if ( $new_width == $FullImage_width && $new_height == $FullImage_height )
copy ( $image_file_path, $new_image_file_path );

$full_id = ImageCreate( $new_width , $new_height ); //create an image
ImageCopyResized( $full_id, $img,
0,0,  0,0, //starting points
$new_width, $new_height,
$FullImage_width, $FullImage_height );
$return_val = ( $full = ImageJPEG( $full_id, $new_image_file_path, 80 )
&& $return_val == 1 ) ? "1" : "0";
ImageDestroy( $full_id );
// --End Creation, Copying--

return ($return_val) ? TRUE : FALSE ;

}
}
$ftmp = $_FILES['image']['tmp_name'];
$oname = $_FILES['image']['name'];
$fname = s('image_folder').'/'.$_FILES['image']['name'];
$tname = s('image_folder').'/tn_'.$oname;
if (resize_jpeg($ftmp,$fname)){
resize_jpeg($ftmp,$tname,125,125) or die("Thumbnail generation failed");
//if(move_uploaded_file($ftmp, $fname)){
?>


exit();
}
?>








?>
Create a new file "resize.php":
This file is used to display the smaller version in the iframe, but can also be used to display a smaller version of your image in your article. (resize.php?pic=filename)
Code: [Select]
if($_GET['pic']){
$img = new img('img/'.$_GET['pic']);
$img->resize();
$img->show();
}

class img {

var $image = '';
var $temp = '';

function img($sourceFile){
if(file_exists($sourceFile)){
$this->image = ImageCreateFromJPEG($sourceFile);
} else {
$this->errorHandler();
}
return;
}

function resize($width = 190, $height = 190, $aspectradio = true){
$o_wd = imagesx($this->image);
$o_ht = imagesy($this->image);
if(isset($aspectradio)&&$aspectradio) {
$w = round($o_wd * $height / $o_ht);
$h = round($o_ht * $width / $o_wd);
if(($height-$h)<($width-$w)){
$width =& $w;
} else {
$height =& $h;
}
}
$this->temp = imageCreateTrueColor($width,$height);
imageCopyResampled($this->temp, $this->image,
0, 0, 0, 0, $width, $height, $o_wd, $o_ht);
$this->sync();
return;
}

function sync(){
$this->image =& $this->temp;
unset($this->temp);
$this->temp = '';
return;
}

function show(){
$this->_sendHeader();
ImageJPEG($this->image);
return;
}

function _sendHeader(){
header('Content-Type: image/jpeg');
}

function errorHandler(){
echo "error";
exit();
}

function store($file){
ImageJPEG($this->image,$file);
return;
}

function watermark($pngImage, $left = 0, $top = 0){
ImageAlphaBlending($this->image, true);
$layer = ImageCreateFromPNG($pngImage);
$logoW = ImageSX($layer);
$logoH = ImageSY($layer);
ImageCopy($this->image, $layer, $left, $top, 0, 0, $logoW, $logoH);
}
}
?>
As of right now, this method only supports .jpgs and is only available when posting a new article. THe script also generates a thumbnail of your image using the same filename preceded by a "tn_".
Logged