Ok so the problem seems to be in the function not creating the image, or in the script it self which does not pass the image to the function
Here is the upload script (note: this is injected into AJAX)
$path = "cache/";
$valid_formats = array("jpg", "png", "gif", "jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
ist($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(2024*2024))
{
$new_file = "header.".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$new_file))
{
echo "<img src='cache/".$new_file."?".time()."' class='preview'>";
createHeader($new_file);
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
now here is the function
function createHeader($new_file) {
$path = "uploads/";
$image = "cache/";
$width = 800;
if(preg_match('/[.](jpg)|(jpeg)$/', $new_file)) {
$im = imagecreatefromjpeg($image . $new_file);
} else if (preg_match('/[.](gif)$/', $new_file)) {
$im = imagecreatefromgif($image . $new_file);
imagecolortransparent($im, black);
} else if (preg_match('/[.](png)$/', $new_file)) {
$im = imagecreatefrompng($image . $new_file);
}
imagealphablending($im, false);
imagesavealpha($im, true);
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $width;
$ny = floor($oy * ($width / $ox) );
imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
imagealphablending($im, true);
if(!file_exists($path)) {
if(!mkdir($path)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path . $new_file, 100);
imagedestroy($im);
imagedestroy($nm);
}
In logic what the code must do is if a user using my cms is loged-in he or she can change the header on the page by clicking an icon which will make a jQuery call to edit.php passing needed query string. From there own user can upload a new image which will be renamed header.jpg and over-write the original image
The header.jpg has to be uploaded into uploads/ directory and a copy of original image cache/ should be unlinked ones the function can create a copy and put it into uploads/ directory.
What is happening is that the first script is uploading the file and is putting it into cache/ directory but the function does not seem to work and does nothing for uploads/ directory.
Any idea what I may be doing wrong or not doing that is causing this annoying problem?
$path = “cache/”;