Jump to content

upload and resize image help (forum img uploads)


samoht

Recommended Posts

Hello all,

 

looking through this forum I found some image resize code and I have tried to integrate it with a joomla forum component  - with a few issues.

 

[*]the png images don't get included in the forum post for some reason - though they do get resized??

[*]I am not able to set the $resized variable so the class works for images not resized??

 

Here is my code:



<?php

// Dont allow direct linking
defined( '_JEXEC' ) or die('Restricted access');

$fbConfig =& CKunenaConfig::getInstance();
require_once(KUNENA_PATH_LIB .DS. 'kunena.helpers.php');
require_once(KUNENA_PATH_LIB .DS. 'kunena.file.class.php');

$attachimage = JRequest::getVar('attachimage', NULL, 'FILES', 'array');
$filename = CKunenaFile::makeSafe($attachimage['name']);


function imageUploadError($msg)
{
    global $message;
    $GLOBALS['KUNENA_rc'] = 0;
    $message = str_replace("[img/]http://", "", $message);
    $app =& JFactory::getApplication();
    $app->enqueueMessage(_IMAGE_NOT_UPLOADED .' ('. $msg .')', 'notice');
}

function imageUploadResize($img)
{
global $message;
// some settings
$max_upload_width = 350;
$max_upload_height = 500;

// if user chosed properly then scale down the image according to user preferances
if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
$max_upload_width = $_REQUEST['max_width_box'];
}   
if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
$max_upload_height = $_REQUEST['max_height_box'];


// if uploaded image was JPG/JPEG
if($img["type"] == "image/jpeg" || $img["type"] == "image/pjpeg"){ 
$image_source = imagecreatefromjpeg($img["tmp_name"]);
}     
// if uploaded image was GIF
if($img["type"] == "image/gif"){ 
$image_source = imagecreatefromgif($img["tmp_name"]);

// BMP doesn't seem to be supported so remove it form above image type test (reject bmps) 
// if uploaded image was BMP
if($_FILES["image_upload_box"]["type"] == "image/bmp"){ 
$image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
}       
// if uploaded image was PNG
if($img["type"] == "image/x-png" || $img["type"] == "image/png"){
$image_source = imagecreatefrompng($img["tmp_name"]);
}


$remote_file = "images/fbfiles/images/".$img["name"];
imagejpeg($image_source,$remote_file,100);
chmod($remote_file,0644);
 

// get width and height of original image
list($image_width, $image_height) = getimagesize($remote_file);

if($image_width>$max_upload_width || $image_height >$max_upload_height){
$proportions = $image_width/$image_height;

if($image_width>$image_height){
$new_width = $max_upload_width;
$new_height = round($max_upload_width/$proportions);
}     
else{
$new_height = $max_upload_height;
$new_width = round($max_upload_height*$proportions);
}     


$new_image = imagecreatetruecolor($new_width , $new_height);
$image_source = imagecreatefromjpeg($remote_file);

imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagejpeg($new_image,$remote_file,100);

imagedestroy($new_image);
}

imagedestroy($image_source);

$resized = 1;

return $resized;

}
$GLOBALS['KUNENA_rc'] = 1; //reset return code
$filename = split("\.", $filename);
//some transaltions for readability
//numExtensions= people tend to upload malicious files using mutliple extensions like: virus.txt.vbs; we'll want to have the last extension to validate against..
$numExtensions = (count($filename)) - 1;
//Translate all invalid characters
$imageName = preg_replace("/[^0-9a-zA-Z_]/", "_", $filename[0]);
// get the final extension
$imageExt = $filename[$numExtensions];
// create the new filename
$newFileName = $imageName . '.' . $imageExt;
// Get the Filesize
$imageSize = $attachimage['size'];

//Enforce it is a new file
if (file_exists(KUNENA_PATH_UPLOADED .DS. "images" .DS. $newFileName)) {
    $newFileName = $imageName . '-' . date('Ymd') . "." . $imageExt;
    for ($i=2; file_exists(KUNENA_PATH_UPLOADED .DS. "images" .DS. $newFileName); $i++) {
    $newFileName = $imageName . '-' . date('Ymd') . "-$i." . $imageExt;
    }
}

if ($GLOBALS['KUNENA_rc'])
{
    //Filename + proper path
    $imageLocation = strtr(KUNENA_PATH_UPLOADED .DS. "images" .DS. $newFileName, "\\", "/");
    $maxImgSize = $fbConfig->imagesize * 1024;

    // Check for empty filename
    if (!is_uploaded_file($attachimage['tmp_name']) || empty($attachimage['name'])) {
        imageUploadError(_IMAGE_ERROR_EMPTY);
    }
    // Check for allowed file type (jpeg, gif, png)
    else if (!($imgtype = KUNENA_check_image_type($imageExt))) {
        imageUploadError(_IMAGE_ERROR_TYPE);
    }
    // Check filesize
    else if ($imageSize > $maxImgSize) {
        //imageUploadError(_IMAGE_ERROR_SIZE . " (" . $fbConfig->imagesize . "kb)");
imageUploadResize($attachimage);
    }
else {
    list($width, $height) = @getimagesize($attachimage['tmp_name']);

    // Check image width
    if ($width > $fbConfig->imagewidth) {
        //imageUploadError(_IMAGE_ERROR_WIDTH . " (" . $fbConfig->imagewidth . " pixels");
imageUploadResize($attachimage);
    }
    // Check image height
    else if ($height > $fbConfig->imageheight) {
        //imageUploadError(_IMAGE_ERROR_HEIGHT . " (" . $fbConfig->imageheight . " pixels");
imageUploadResize($attachimage);
    }
}
}

if ($GLOBALS['KUNENA_rc'])
{

if(!$resized){
// this should be my new resized image path to put into the forum!
    $code = '[img=' . KUNENA_LIVEUPLOADEDPATH. 'images/' . $newFileName . ']';

    if (preg_match("/\[img\/\]http://www.phpfreaks.com/si", $message)) {
        $message = str_replace("[img/]", $code, $message);
    }
    else {
        $message = $message . ' ' . $code;
    }
}else{
// file is OK, move it to the proper location
CKunenaFile::upload($attachimage['tmp_name'], $imageLocation);

// echo '<span class="contentheading">'._IMAGE_UPLOADED."...</span>";
    $code = '[img=' . KUNENA_LIVEUPLOADEDPATH. 'images/' . $newFileName . ']';

    if (preg_match("/\[img\/\]/si", $message)) {
        $message = str_replace("[img/]", $code, $message);
    }
    else {
        $message = $message . ' ' . $code;
    }
}
}
?>

 

the function imageUploadResize() is what I added + the conditional in the return html code.

 

Thanks for any help,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.