Hi Guys,
I have 2 sites a US and a UK based site, i use the admin panel in the UK to control both sites including uploading images etc.
when adding images if it comes from the US site it resizes and then watermarks and the same for the UK site.
Resize & Watermark Functions:
function resize_image($uploadDirectory, $newFileName, $us = false)
{
$original_image = $uploadDirectory;
$ext = substr($original_image, strrpos($original_image, '.') + 1);
$canvas_width = 65;
$canvas_height = 65;
$canvas = imagecreatetruecolor($canvas_width, $canvas_height);
$white_background = imagecolorallocate($canvas, 255, 255, 255);
imagefill($canvas, 0, 0, $white_background);
list($image_width, $image_height) = getimagesize($uploadDirectory);
$ratio = $image_width / $image_height;
if ($ratio > 1) {
$new_image_width = 65;
$new_image_height = 65 / $ratio;
} //$ratio > 1
else {
$new_image_width = (float) 65 * $ratio;
$new_image_height = 65;
}
if ($ext == "jpg") {
$original_image = imagecreatefromjpeg($original_image);
} //$ext == "jpg"
if ($ext == "gif") {
$original_image = imagecreatefromgif($original_image);
} //$ext == "gif"
imagecopyresampled($canvas, $original_image, 0, 0, 0, 0, $new_image_width, $new_image_height, $image_width, $image_height);
$new_thumbnail_name = "thumb-$newFileName";
if ($ext == "jpg") {
if ($us)
{
imagejpeg($canvas, "../../first-choice-pharmacy.com/imgProducts/img-th/$new_thumbnail_name", 100);
return ("$new_thumbnail_name");
} else {
imagejpeg($canvas, "../imgProducts/img-th/$new_thumbnail_name", 100);
return ("$new_thumbnail_name");
}
} //$ext == "jpg"
if ($ext == "gif") {
if ($us)
{
imagegif($canvas, "../../first-choice-pharmacy.com/imgProducts/img-th/$new_thumbnail_name", 100);
return ("$new_thumbnail_name");
} else {
imagegif($canvas, "../imgProducts/img-th/$new_thumbnail_name", 100);
return ("$new_thumbnail_name");
}
} //$ext == "gif"
imagedestroy($original_image);
imagedestroy($canvas);
}
function watermark_image($uploadDirectory, $newFileName, $us = false)
{
$original_image = $uploadDirectory;
$ext = substr($original_image, strrpos($original_image, '.') + 1);
// Load the image where the logo will be embeded into
if ($ext == "jpg") {
$image = imagecreatefromjpeg($uploadDirectory);
} //$ext == "jpg"
if ($ext == "gif") {
$image = imagecreatefromgif($uploadDirectory);
} //$ext == "jpg"
// Load the logo image
$logoImage = imagecreatefrompng("../images/logo.png");
imagealphablending($logoImage, true);
// Get dimensions
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
// Get dimensions
$logoWidth = imagesx($logoImage);
$logoHeight = imagesy($logoImage);
// Paste the logo
imagecopy(
// source
$image,
// destination
$logoImage,
// destination x and y
$imageWidth-$logoWidth, $imageHeight-$logoHeight,
// source x and y
0, 0,
// width and height of the area of the source to copy
$logoWidth, $logoHeight);
if ($ext == "jpg") {
if (isset($us))
{
imagejpeg($image, "../../first-choice-pharmacy.com/imgProducts/img-fs/$newFileName", 100);
} else {
imagejpeg($image, "../imgProducts/img-fs/$newFileName", 100);
}
} //$ext == "jpg"
if ($ext == "gif") {
if (isset($us))
{
imagegif($image, "../../first-choice-pharmacy.com/imgProducts/img-fs/$newFileName", 100);
} else {
imagegif($image, "../imgProducts/img-fs/$newFileName", 100);
}
} //$ext == "jpg"
imagedestroy($original_image);
imagedestroy($canvas);
}
Upload code:
<?php
if (isset($_POST['submit-add-product']))
{
$productNM = ucwords($_POST['txt-product-name']);
$productDS = escape_data($_POST['txt-product-description'], 1);
$productPM = $_POST['txt-product-p-medicine'];
$productCT = $_POST['txt-product-category'];
$productPR = $_POST['txt-product-price'];
$productST = $_POST['txtSite'];
$fileName = $_FILES['txt-product-image']['name'];
$fileTemp = $_FILES['txt-product-image']['tmp_name'];
$fileType = $_FILES['txt-product-image']['type'];
$fileSize = $_FILES['txt-product-image']['size'];
/*if (!empty($_FILES['txt-product-image-2']['name'])) {
$fileName2nd = $_FILES['txt-product-image-2']['name'];
$fileTemp2nd = $_FILES['txt-product-image-2']['tmp_name'];
$fileType2nd = $_FILES['txt-product-image-2']['type'];
$fileSize2nd = $_FILES['txt-product-image-2']['size'];
$fileExtension = substr(strrchr($fileName2nd, '.'), 0);
$fileExtension = strtolower($fileExtension);
$replacedName2nd = str_replace(" ", "-", $productNM . "-2nd");
$newFileName2nd = "$replacedName2nd-" . time() . "$fileExtension";
$uploadDirectory2nd = "../imgProducts/img-fs/$newFileName2nd";
$generate2ndImage = true;
} //!empty($_FILES['txt-product-image-2']['name'])
if (empty($productNM)) {
print "<p class=\\"fcp-message-error\\">You never entered a product name.</p>";
} //empty($productNM)
if (empty($productDS)) {
print "<p class=\\"fcp-message-error\\">You never entered a product description.</p>";
} //empty($productDS)
if (empty($productPR)) {
print "<p class=\\"fcp-message-error\\">You never entered a product price.</p>";
} //empty($productPR)
if (empty($fileSize)) {
print "<p class=\\"fcp-message-error\\">You never entered a product image.</p>";
} //empty($fileSize)
if (!isImageFile($fileType)) {
print "<p class=\\"fcp-message-error\\">The file you uploaded doesn't seem to be an image.</p>";
} //!isImageFile($fileType)*/
$fileExtension = substr(strrchr($fileName, '.'), 0);
$fileExtension = strtolower($fileExtension);
$replacedName = str_replace(" ", "-", $productNM);
$newFileName = "$replacedName-" . time() . "$fileExtension";
$uploadDirectory = "../imgProducts/img-fs/$newFileName";
// US //
if ($productST == "US")
{
$uploadDirectory = "../../first-choice-pharmacy.com/imgProducts/img-fs/$newFileName";
//move_uploaded_file($fileTemp, $uploadDirectory2nd);
if (move_uploaded_file($fileTemp, $uploadDirectory)) {
$productWM = watermark_image($uploadDirectory, $newFileName, 1);
$productTH = resize_image($uploadDirectory, $newFileName, 1);
} //move_uploaded_file($fileTemp, $uploadDirectory)
/*if (isset($generate2ndImage)) {
watermark_image($uploadDirectory2nd, $fileTemp2nd);
move_uploaded_file($fileTemp2nd, $uploadDirectory2nd);
} //isset($generate2ndImage)*/
$qI = mysql_query("INSERT INTO `fcpv3_products` (`id`,`category_id`,`product_name`,`product_description`,`product_thumbnail`,`product_fullsize`,`product_fullsize_second`,`product_price`,`product_weight`,`p_med`,`is_breakable`,`site`,`date_added`) VALUES ('','$productCT','$productNM','$productDS','$productTH','$newFileName','$newFileName2nd','$productPR','0.00','$productPM','N','US',NOW())");
if ($_POST['updateTwitter'] == "yes") {
$last_product_id = mysql_insert_id();
$twitter_url = generate_seo_friendly_links($productNM, $last_product_id);
$username = 'firstchoicephar';
$password = 'milkybar';
$status = urlencode(stripslashes(urldecode('We just added ' . $productNM . ' http://www.firstchoicepharmacy.co.uk/product.php?id=' . mysql_insert_id())));
if ($status) {
$tweetUrl = 'http://www.twitter.com/statuses/update.xml';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$tweetUrl");
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);
if ($resultArray['http_code'] == 200) {
print "<p class=\\"fcp-message-success\\">Tweet made successfully :).</p>";
} //$resultArray['http_code'] == 200
else {
print '<p class="fcp-message-error">OOPS, an error has occured Posting to twitter, please contact the site administrator.</p>';
curl_close($curl);
}
} //$status
if ($qI) {
print "<p class=\\"fcp-message-success\\">You have successfully added a new product.</p>";
} //$qI
else {
print '<p class="fcp-message-error">OOPS, an error has occured please contact the site administrator.</p>';
}
} //$_POST['updateTwitter'] == "yes"
} elseif($productST == "UK") {
if (move_uploaded_file($fileTemp, $uploadDirectory)) {
$productWM = watermark_image($uploadDirectory, $newFileName);
$productTH = resize_image($uploadDirectory, $newFileName);
} //move_uploaded_file($fileTemp, $uploadDirectory)
/*if (isset($generate2ndImage)) {
watermark_image($uploadDirectory2nd, $fileTemp2nd);
move_uploaded_file($fileTemp2nd, $uploadDirectory2nd);
} //isset($generate2ndImage)*/
$qI = mysql_query("INSERT INTO `fcpv3_products` (`id`,`category_id`,`product_name`,`product_description`,`product_thumbnail`,`product_fullsize`,`product_fullsize_second`,`product_price`,`product_weight`,`p_med`,`is_breakable`,`site`,`date_added`) VALUES ('','$productCT','$productNM','$productDS','$productTH','$newFileName','$newFileName2nd','$productPR','0.00','$productPM','N','UK',NOW())");
if ($_POST['updateTwitter'] == "yes") {
$last_product_id = mysql_insert_id();
$twitter_url = generate_seo_friendly_links($productNM, $last_product_id);
$username = 'firstchoicephar';
$password = '';
$status = urlencode(stripslashes(urldecode('We just added ' . $productNM . ' http://www.firstchoicepharmacy.co.uk/product.php?id=' . mysql_insert_id())));
if ($status) {
$tweetUrl = 'http://www.twitter.com/statuses/update.xml';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$tweetUrl");
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);
if ($resultArray['http_code'] == 200) {
print "<p class=\\"fcp-message-success\\">Tweet made successfully :).</p>";
} //$resultArray['http_code'] == 200
else {
print '<p class="fcp-message-error">OOPS, an error has occured Posting to twitter, please contact the site administrator.</p>';
curl_close($curl);
}
} //$status
if ($qI) {
print "<p class=\\"fcp-message-success\\">You have successfully added a new product.</p>";
} //$qI
else {
print '<p class="fcp-message-error">OOPS, an error has occured please contact the site administrator.</p>';
}
} //$_POST['updateTwitter'] == "yes"
}
} //isset($_POST['submit-add-product'])
?>
So when i do $productWM = watermark_image($uploadDirectory, $newFileName, 1); this is for the US site so use the US path and location specified, where as the UK one looks like $productWM = watermark_image($uploadDirectory, $newFileName);
If i tinker with the code i get one to work and not the other and vice versa it seems to be my logic in the resize function that messes things up.
a fresh pair of eyes would be great
thanks guys
Graham