PHP 8.0 Ajax Upload compatibility issues - php

I have some issues with my upload.php code.
It works fine with PHP 7.4.
Only Notice is: "Undefined index: submitbtn".
However in PHP 8.0 it turns to a Warning: "Undefined array key "submitbtn", and the code does not execute.
Basically the code does:
Upload an image (html5 input) to folder: /uploads - without refresh!
Rotate the image if needed (needs to be vertical) - because some capture with mobile-phone and the photo gets horizontal aligned when uploaded for some reason.
Compress the image to folder: /compresseduploads.
Echo' some Javascript to exucute (a way for me to get the new /compresseduploads image path).
I really hope you can help me get it compatible with the new PHP version. Thanks :-)
(I dont think imagerotate() works with PHP 8.1 yet - therefore I am aiming for PHP 8.0.)
pssstt.. any other improvements is also welcome as I am very new to PHP.
CODE:
Index.php (input)
<form class="uploadform" method="post" enctype="multipart/form-data"
action="/wp-content/plugins/Painting_Preview/upload.php" accept="image/*" capture="camera" />
<input type="file" id="imgInp" class="span10" value="Capture photo" name="imagefile" />
<input type="submit" value="Submit" name="submitbtn" id="submitbtn">
</form>
Upload.php (compress, rotate, upload and echo js)
<?php
$file_formats = array("jpg", "jpeg", "png", "gif", "bmp"); // Set File format
$filepath = "uploads/";
if ($_POST['submitbtn']=="Submit") {
$name = $_FILES['imagefile']['name'];
$size = $_FILES['imagefile']['size'];
if (strlen($name)) {
$extension = substr($name, strrpos($name, '.')+1);
if (in_array($extension, $file_formats)) {
if ($size < 1500000000) {
$imagename = md5(uniqid().time()).".".$extension;
$tmp = $_FILES['imagefile']['tmp_name'];
if (move_uploaded_file($tmp, $filepath . $imagename)) {
echo 'THIS IS JUST THE JAVASCRIPT....
<script>jQuery(document).ready(function(.....</script>';
function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source);
////FIX ROTATE///
# Get exif information
$exif = exif_read_data($source);
# Add some error handling
# Get orientation
$orientation = $exif['Orientation'];
# Manipulate image
switch ($orientation) {
case 2:
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 3:
$imageObject = imagerotate($image, 180, 0);
break;
case 4:
imageflip($image, IMG_FLIP_VERTICAL);
break;
case 5:
$imageObject = imagerotate($image, -90, 0);
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 7:
$image = imagerotate($imageObject, 90, 0);
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 8:
$image = imagerotate($imageObject, 90, 0);
break;
}
}
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
$src = 'uploads/'.$imagename;
$dest = 'compresseduploads/'.$imagename;
compress($src, $dest, $quality=30);
} else {
echo "Error. Please try again.";
}
} else {
echo "Error - Your image exceeds 15 MB.";
}
} else {
echo "Invalid file format - please use .jpg or .png.";
}
} else {
echo ".....";
}
exit();
}
?>

Related

why imagecreatefromjpeg() function is not opening the file from the folder

I want to resize the image file after upload with the help of imagecreatefromjpeg function but this function is unable to access the file from the folder as it's throwing the error i.e., **
imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable
error: in D:\xampp\htdocs\resize\index.php
** but file is uploaded & I wrote the following code.
<form method="post" enctype="multipart/form-data">
<input type="file" name="f1">
<input type="submit" name="btn" value="Upload">
</form>
<?php
ini_set("memory_limit","256M");
if(isset($_POST['btn']))
{
if(move_uploaded_file($_FILES['f1']['tmp_name'], "images/".$_FILES['f1']['name']))
{
$filename = "images/".$_FILES['f1']['name'];
$original_info = getimagesize($filename);
$original_w = $original_info[0];
$original_h = $original_info[1];
echo "<img src =$filename>";
if( ini_get('allow_url_fopen') ) {
// it's enabled, so do something
$original_img = imagecreatefromjpeg($filename);
$thumb_w = 100;
$thumb_h = 60;
$thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
$thumb_filename = "new.jpg";
imagecopyresampled($thumb_img, $original_img,
0, 0,
0, 0,
$thumb_w, $thumb_h,
$original_w, $original_h);
imagejpeg($thumb_img, $thumb_filename);
imagedestroy($thumb_img);
imagedestroy($original_img);
}
}
}
?>
You must make sure of the type of file.
<?php
$type = exif_imagetype($filename);
// types 1=>gif, 2=>jpg, 3=>png, 6=>bmp
switch ($type) {
case 1 : $img = imageCreateFromGif ( $src );break;
case 2 : $img = imageCreateFromJpeg( $src );break;
case 3 : $img = imageCreateFromPng ( $src );break;
case 6 : $img = imageCreateFromBmp ( $src );break;
}
// or if you cannot use exif_imagetype
// you can use getImageSize
$type = getImageSize($filename);
switch ($type) {
case 'image/gif' : $img = imageCreateFromGif ( $filename ); break;
case 'image/jpeg' : $img = imageCreateFromJpeg( $filename ); break;
case 'image/png' : $img = imageCreateFromPng ( $filename ); break;
case 'image/bmp' : $img = imageCreateFromBmp ( $filename ); break;
}
//// than you can create new file with resize or crop...
?>

Convert jpg to webp using imagewebp

I'm having trouble using imagewebp to convert an image to webp.
I use this code:
$filename = dirname(__FILE__) .'/example.jpg';
$im = imagecreatefromjpeg($filename);
$webp =imagewebp($im, str_replace('jpg', 'webp', $filename));
imagedestroy($im);
var_dump($webp);
$webp returns true but when I try to view the webp-image in Chrome it just shows blank, but with the correct size. If I instead load the image and set headers with PHP (see below) it shows up, but with wrong colors (too much yellow).
$im = imagecreatefromwebp('example.webp');
header('Content-Type: image/webp');
imagewebp($im);
imagedestroy($im);
If I convert the same image with command line it works as expected.
cwebp -q 100 example.jpg -o example.webp
I'm testing this on Ubuntu 14, Apache 2.4.7 and PHP 5.5.9-1ubuntu4.4.
It works:
$jpg=imagecreatefromjpeg('filename.jpg');
$w=imagesx($jpg);
$h=imagesy($jpg);
$webp=imagecreatetruecolor($w,$h);
imagecopy($webp,$jpg,0,0,0,0,$w,$h);
imagewebp($webp, 'filename.webp', 80);
imagedestroy($jpg);
imagedestroy($webp);
I had same problem, my solution is:
$file='hnbrnocz.jpg';
$image= imagecreatefromjpeg($file);
ob_start();
imagejpeg($image,NULL,100);
$cont= ob_get_contents();
ob_end_clean();
imagedestroy($image);
$content = imagecreatefromstring($cont);
imagewebp($content,'images/hnbrnocz.webp');
imagedestroy($content);
I user this
function webpConvert2($file, $compression_quality = 80)
{
// check if file exists
if (!file_exists($file)) {
return false;
}
$file_type = exif_imagetype($file);
//https://www.php.net/manual/en/function.exif-imagetype.php
//exif_imagetype($file);
// 1 IMAGETYPE_GIF
// 2 IMAGETYPE_JPEG
// 3 IMAGETYPE_PNG
// 6 IMAGETYPE_BMP
// 15 IMAGETYPE_WBMP
// 16 IMAGETYPE_XBM
$output_file = $file . '.webp';
if (file_exists($output_file)) {
return $output_file;
}
if (function_exists('imagewebp')) {
switch ($file_type) {
case '1': //IMAGETYPE_GIF
$image = imagecreatefromgif($file);
break;
case '2': //IMAGETYPE_JPEG
$image = imagecreatefromjpeg($file);
break;
case '3': //IMAGETYPE_PNG
$image = imagecreatefrompng($file);
imagepalettetotruecolor($image);
imagealphablending($image, true);
imagesavealpha($image, true);
break;
case '6': // IMAGETYPE_BMP
$image = imagecreatefrombmp($file);
break;
case '15': //IMAGETYPE_Webp
return false;
break;
case '16': //IMAGETYPE_XBM
$image = imagecreatefromxbm($file);
break;
default:
return false;
}
// Save the image
$result = imagewebp($image, $output_file, $compression_quality);
if (false === $result) {
return false;
}
// Free up memory
imagedestroy($image);
return $output_file;
} elseif (class_exists('Imagick')) {
$image = new Imagick();
$image->readImage($file);
if ($file_type === "3") {
$image->setImageFormat('webp');
$image->setImageCompressionQuality($compression_quality);
$image->setOption('webp:lossless', 'true');
}
$image->writeImage($output_file);
return $output_file;
}
return false;
}

How can I convert all images to jpg?

I have script:
<?php
include('db.php');
session_start();
$session_id = '1'; // User session id
$path = "uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg");
if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if (strlen($name)) {
list($txt, $ext) = explode(".", $name);
if (in_array($ext, $valid_formats)) {
if ($size < (1024 * 1024)) { // Image size max 1 MB
$actual_image_name = time() . $session_id . "." . $ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if (move_uploaded_file($tmp, $path . $actual_image_name)) {
mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
echo "<img src='uploads/" . $actual_image_name . "' class='preview'>";
} else {
echo "failed";
}
} else {
echo "Image file size max 1 MB";
}
} else {
echo "Invalid file format..";
}
} else {
echo "Please select image..!";
}
exit;
}
?>
Is possible convert all images (png, gif etc) to jpg with 100% quality? If yes, how? I would like allow to upload png and gif, but this script should convert this files to jpg. Is possible this with PHP?
Try this code: originalImage is the path of... the original image... outputImage is self explaining enough. Quality is a number from 0 to 100 setting the output jpg quality (0 - worst, 100 - best)
function convertImage($originalImage, $outputImage, $quality)
{
// jpg, png, gif or bmp?
$exploded = explode('.',$originalImage);
$ext = $exploded[count($exploded) - 1];
if (preg_match('/jpg|jpeg/i',$ext))
$imageTmp=imagecreatefromjpeg($originalImage);
else if (preg_match('/png/i',$ext))
$imageTmp=imagecreatefrompng($originalImage);
else if (preg_match('/gif/i',$ext))
$imageTmp=imagecreatefromgif($originalImage);
else if (preg_match('/bmp/i',$ext))
$imageTmp=imagecreatefrombmp($originalImage);
else
return 0;
// quality is a value from 0 (worst) to 100 (best)
imagejpeg($imageTmp, $outputImage, $quality);
imagedestroy($imageTmp);
return 1;
}
Try using Imagick setImageFormat, for me it provides the best image quality
http://php.net/manual/en/imagick.setimageformat.php
$im = new imagick($image);
// convert to png
$im->setImageFormat('png');
//write image on server
$im->writeImage($image .".png");
$im->clear();
$im->destroy();
Davide Berra's answer is great, so I improved the file type detection a little, using exif_imagetype() instead of relying on the file extension:
/**
* Auxiliar function to convert images to JPG
*/
function convertImage($originalImage, $outputImage, $quality) {
switch (exif_imagetype($originalImage)) {
case IMAGETYPE_PNG:
$imageTmp=imagecreatefrompng($originalImage);
break;
case IMAGETYPE_JPEG:
$imageTmp=imagecreatefromjpeg($originalImage);
break;
case IMAGETYPE_GIF:
$imageTmp=imagecreatefromgif($originalImage);
break;
case IMAGETYPE_BMP:
$imageTmp=imagecreatefrombmp($originalImage);
break;
// Defaults to JPG
default:
$imageTmp=imagecreatefromjpeg($originalImage);
break;
}
// quality is a value from 0 (worst) to 100 (best)
imagejpeg($imageTmp, $outputImage, $quality);
imagedestroy($imageTmp);
return 1;
}
You must have php_exif extension enabled to use this.
A small code to convert image.png to image.jpg at desired image quality:
<?php
$image = imagecreatefrompng('image.png');
imagejpeg($image, 'image.jpg', 70); // 0 = worst / smaller file, 100 = better / bigger file
imagedestroy($image);
?>
a small fix to davide's answer, the correct function for converting from BMP is "imagecreatefromwbmp" instead of imagecreatefrombmp (missing "w")
also you should consider that png may be transparent, here is a way to fill it with white BG (jpeg can't apply alpha data).
function convertImage($originalImage, $outputImage, $quality){
// jpg, png, gif or bmp?
$exploded = explode('.',$originalImage);
$ext = $exploded[count($exploded) - 1];
if (preg_match('/jpg|jpeg/i',$ext)){$imageTmp=imagecreatefromjpeg($originalImage);}
else if (preg_match('/png/i',$ext)){$imageTmp=imagecreatefrompng($originalImage);}
else if (preg_match('/gif/i',$ext)){$imageTmp=imagecreatefromgif($originalImage);}
else if (preg_match('/bmp/i',$ext)){$imageTmp=imagecreatefromwbmp($originalImage);}
else { return false;}
// quality is a value from 0 (worst) to 100 (best)
imagejpeg($imageTmp, $outputImage, $quality);
imagedestroy($imageTmp);
return true;
}
From PhpTools:
/**
* #param string $source (accepted jpg, gif & png filenames)
* #param string $destination
* #param int $quality [0-100]
* #throws \Exception
*/
public function convertToJpeg($source, $destination, $quality = 100) {
if ($quality < 0 || $quality > 100) {
throw new \Exception("Param 'quality' out of range.");
}
if (!file_exists($source)) {
throw new \Exception("Image file not found.");
}
$ext = pathinfo($source, PATHINFO_EXTENSION);
if (preg_match('/jpg|jpeg/i', $ext)) {
$image = imagecreatefromjpeg($source);
} else if (preg_match('/png/i', $ext)) {
$image = imagecreatefrompng($source);
} else if (preg_match('/gif/i', $ext)) {
$image = imagecreatefromgif($source);
} else {
throw new \Exception("Image isn't recognized.");
}
$result = imagejpeg($image, $destination, $quality);
if (!$result) {
throw new \Exception("Saving to file exception.");
}
imagedestroy($image);
}

Renaming Files by adding a Suffix

Can i change the name of the file, say an image for example which has name like '0124.jpg',
before sending it to the server?
<input id="f1" style="margin-bottom: 5px;" type="file" name="f1" onchange="javascript:readURL_f1(this);"/>
<input id="f2" style="margin-bottom: 5px;" type="file" name="f1" onchange="javascript:readURL_f2(this);"/>
If the file is uploaded from f1, then before sending to to server, the name should become pic1_[filename].jpg, instead of just sending the raw filename.
I don't want this to be done in server side because i think it might be complicated.
EDIT : Upload.php is my php file which uploads whatever in the file. So, its been a challenge for me. i can change the filename, but that gets changed in all the three uploads.
for example i add an '_' for the incoming filename. Then, it gets changed to all filenames.
Anyway from clientside?
my upload script: Upload.php
upload.php
<?php mysql_connect('localhost','root','phpmyadmin');
$connected = mysql_select_db('image_Upload');
?>
<noscript>
<div align="center">Go Back To Upload Form</div><!-- If javascript is disabled -->
</noscript>
<?php
//If you face any errors, increase values of "post_max_size", "upload_max_filesize" and "memory_limit" as required in php.ini
//Some Settings
$ThumbSquareSize = 200; //Thumbnail will be 200x200
$BigImageMaxSize = 500; //Image Maximum height or width
$ThumbPrefix = "thumb_"; //Normal thumb Prefix
$DestinationDirectory = 'uploads/'; //Upload Directory ends with / (slash)
$Quality = 90;
$id = 'm123';
//ini_set('memory_limit', '-1'); // maximum memory!
foreach($_FILES as $file)
{
// some information about image we need later.
$ImageName = $file['name'];
$ImageSize = $file['size'];
$TempSrc = $file['tmp_name'];
$ImageType = $file['type'];
if (is_array($ImageName))
{
$c = count($ImageName);
echo '<ul>';
for ($i=0; $i < $c; $i++)
{
$processImage = true;
$RandomNumber = rand(0, 9999999999); // We need same random name for both files.
if(!isset($ImageName[$i]) || !is_uploaded_file($TempSrc[$i]))
{
echo '<div class="error">Error occurred while trying to process <strong>'.$ImageName[$i].'</strong>, may be file too big!</div>'; //output error
}
else
{
//Validate file + create image from uploaded file.
switch(strtolower($ImageType[$i]))
{
case 'image/png':
$CreatedImage = imagecreatefrompng($TempSrc[$i]);
break;
case 'image/gif':
$CreatedImage = imagecreatefromgif($TempSrc[$i]);
break;
case 'image/jpeg':
case 'image/pjpeg':
$CreatedImage = imagecreatefromjpeg($TempSrc[$i]);
break;
default:
$processImage = false; //image format is not supported!
}
//get Image Size
list($CurWidth,$CurHeight)=getimagesize($TempSrc[$i]);
//Get file extension from Image name, this will be re-added after random name
$ImageExt = substr($ImageName[$i], strrpos($ImageName[$i], '.'));
$ImageExt = str_replace('.','',$ImageExt);
//Construct a new image name (with random number added) for our new image.
$NewImageName = $id.'_'.'pic'.($i+1).'.'.$ImageExt;
//Set the Destination Image path with Random Name
$thumb_DestRandImageName = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumb name
$DestRandImageName = $DestinationDirectory.$NewImageName; //Name for Big Image
//Resize image to our Specified Size by calling resizeImage function.
if($processImage && resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType[$i]))
{
//Create a square Thumbnail right after, this time we are using cropImage() function
if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType[$i]))
{
echo 'Error Creating thumbnail';
}
/*
At this point we have succesfully resized and created thumbnail image
We can render image to user's browser or store information in the database
For demo, we are going to output results on browser.
*/
//Get New Image Size
list($ResizedWidth,$ResizedHeight)=getimagesize($DestRandImageName);
echo '<table width="100%" border="0" cellpadding="4" cellspacing="0">';
echo '<tr>';
echo '<td align="center"><img src="uploads/'.$ThumbPrefix.$NewImageName.
'" alt="Thumbnail" height="'.$ThumbSquareSize.'" width="'.$ThumbSquareSize.'"></td>';
echo '</tr><tr>';
echo '<td align="center"><img src="uploads/'.$NewImageName.
'" alt="Resized Image" height="'.$ResizedHeight.'" width="'.$ResizedWidth.'"></td>';
echo '</tr>';
echo '</table>';
if(isset($id))
{
mysql_query("UPDATE imagetable SET ImageName='$DestRandImageName',ThumbName='$thumb_DestRandImageName',
ImgPath='uploads/' WHERE id='$id'");
}
else{
mysql_query("INSERT INTO imagetable (id, ImageName, ThumbName, ImgPath)
VALUES ('$id','$DestRandImageName', '$thumb_DestRandImageName', 'uploads/')");
}
}else{
echo '<div class="error">Error occurred while trying to process <strong>'.$ImageName.
'</strong>! Please check if file is supported</div>';
}
}
}
echo '</ul>';
}
}
// This function will proportionally resize image
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0)
{
return false;
}
//Construct a proportional size of new image
$ImageScale = min($MaxSize/$CurWidth, $MaxSize/$CurHeight);
$NewWidth = ceil($ImageScale*$CurWidth);
$NewHeight = ceil($ImageScale*$CurHeight);
if($CurWidth < $NewWidth || $CurHeight < $NewHeight)
{
$NewWidth = $CurWidth;
$NewHeight = $CurHeight;
}
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
// Resize Image
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
{
switch(strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
if(is_resource($NewCanves)) {
imagedestroy($NewCanves);
}
return true;
}
}
//This function corps image to create exact square images, no matter what its original size!
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0)
{
return false;
}
if($CurWidth>$CurHeight)
{
$y_offset = 0;
$x_offset = ($CurWidth - $CurHeight) / 2;
$square_size = $CurWidth - ($x_offset * 2);
}else{
$x_offset = 0;
$y_offset = ($CurHeight - $CurWidth) / 2;
$square_size = $CurHeight - ($y_offset * 2);
}
$NewCanves = imagecreatetruecolor($iSize, $iSize);
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size))
{
switch(strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
if(is_resource($NewCanves)) {
imagedestroy($NewCanves);
}
return true;
}
}
When you are uploading the File you usually follow this workflow
User Chooses a File and Clicks Upload
Server Pics up the File from the temp folder - check MimeType, Resize and Rename the File and store it to which ever location you want on the file server.
While Renaming if you want to see if the same file name exsists and then append the _01, _02 then you will have to check if a file exists with that name and then append the unique number at the end.
Things like that are usually done in the server side. The purpose of preppending or appending a code or, say, changing the whole name of the file is to prevent uploaded file name conflicts. So imagine doing it to the client side? When upload button is pressed you need to ask the server if this file name is already existing, then you wait for the response of the server then do the renaming based on the response then send it to the server instead of just sending it to the server then do the checking then renaming then saving.

Problem processing php and uploading file, uploadify script

I have a problem with php uploadify script.
I`m using single uploader, non multi.
Here is uploadify.php file which I edited:
<?php
// Callin
require_once("../../includes/functions.php");
connect_db();
check();
// If file exists
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION); //figures out the extension
$newFileName = md5($tempFile).'.'.$ext; //generates random filename, then adds the file extension
$targetFile = str_replace('//','/',$targetPath) . $newFileName;
if($ext == "jpg" || $ext == "gif" || $ext == "png") {
$imgsize = getimagesize($targetFile);
switch(strtolower(substr($targetFile, -3)))
{
case "jpg":
$image = imagecreatefromjpeg($targetFile);
break;
case "png":
$image = imagecreatefrompng($targetFile);
break;
case "gif":
$image = imagecreatefromgif($targetFile);
break;
default:
exit;
break;
}
$width = 180;
$height = 135; // Don't need proportional
$src_w = $imgsize[0];
$src_h = $imgsize[1];
$picture = imagecreatetruecolor($width, $height);
imagealphablending($picture, false);
imagesavealpha($picture, true);
$bool = imagecopyresampled($picture, $image, 0, 0, 0, 0, $width, $height, $src_w, $src_h);
if($bool)
{
switch(strtolower(substr($targetFile, -3)))
{
case "jpg":
header("Content-Type: image/jpeg");
$bool2 = imagejpeg($picture,$targetPath."../../galleries/vthumbs/".$_FILES['Filedata']['name'],80); // Two folders back to public_html, and /galleries/vthumbs/
break;
case "png":
header("Content-Type: image/png");
imagepng($picture,$targetPath."../../galleries/vthumbs/".$_FILES['Filedata']['name']); // Two folders back to public_html, and /galleries/vthumbs/
break;
case "gif":
header("Content-Type: image/gif");
imagegif($picture,$targetPath."../../galleries/vthumbs/".$_FILES['Filedata']['name']); // Two folders back to public_html, and /galleries/vthumbs/
break;
}
}
imagedestroy($picture);
imagedestroy($image);
}
else {
// If extension isn't jpg or gif or png just move file
move_uploaded_file($tempFile,$targetFile);
}
echo '0'; // Required to trigger onComplete function
}
else { // Required to trigger onComplete function
echo '1'; // For tests
// mysql_query .........
}
?>
What I'm trying to make?
I want to make uploader which upload videos and images files.
If it's image I want to make just thumbnail. i don't care about full image, if isn't image file, that must be video and just upload video.
I thnik that php code works fine, uploadify script don't have callback errors from uploadify.php, but when I change if (!empty($_FILES)) to if (empty($_FILES)) page shows me 0. By this, I thnik that php code is OK, and there is no errors.
But files isn't uploaded. I checked for folder CHMODs and others. All is OK.
Script works with default uploadify.php code.
uploadify.php which I edited:
<?php
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
echo '0'; // Required to trigger onComplete function
}
else { // Required to trigger onComplete
echo '1';
}
?>
If need, I will paste javascript code with I calling uploadify.php, but I Thnik that is no nessesary, because script works with default uploadify.php and with edited uploadify.php no.
All help is welcome...
Replace if (!empty($_FILES)) with if (sizeof($_FILES) > 0)

Categories