Please help this issue is all that is prevenying site completion.

Discussion in 'PHP' started by Sleeping Troll, Oct 1, 2008.

  1. #1
    I have written a site that is for designing custom vinyl banners it allows client to provide a photo and/or Text for the banner, It creates 3 gd images 1 for basic template, 1 for text and 1 for photo. the text and photo images have alpha transparency and all 3 are saved as .png. The text and photo images are copied to the template image to produce a preview of the banner. 1 example is called "Happy birthday" it has both text and photo and it works fine. The other is called Welcome it contains only text and the problem is there its preview shows only the basic template and does not show the text! The code below is for the text update and the photo update, in that order. I can view the images on the server and they are just fine, but it would seem that the "overlays for text and photo are not being copied to template in the case of "Welcome" banner, however I get no errors thrown. please visit site at http://trollnest.com/bragflags/default.php Try Happy birthday(working properly) then try Welcome (not updating preview). IM me if you think you can help, .

    <?php
    $conn = mysql_connect("xxx","xxx","xxx"); 
    mysql_select_db("Brag_Flags",$conn);
    $ClientID = $_COOKIE["Brag_Flag_user"];
    $TextID = $_GET["TextID"];
    $Text = $_GET["Text"];
    $SQL="Select * From Projects where ClientID = $ClientID and Status = 'Current'";
    $ProjectInfo = mysql_query($SQL);
    $Info = mysql_fetch_array($ProjectInfo);
    $SQL="Update PreviewText Set Text = '$Text' Where ProjectID =".$Info['ProjectID']." and TextID = $TextID";
    mysql_query($SQL);
    
    //Get Project Info
    $SQL="Select * From Projects where ClientID = $ClientID and Status = 'Current'";
    $ProjectInfo = mysql_query($SQL);
    $Info = mysql_fetch_array($ProjectInfo);
    
    //Scaling to match Preview
    $Inch = 24;
    
    //Create Text Overlay
    $overlay = imagecreatefrompng("Images/Overlay_Blanks/24x36.png");
    
    //Get Stored Text
    $SQL="Select * From PreviewText Where ProjectID =".$Info['ProjectID'];
    $StoredText = mysql_query($SQL);
    while($Text = mysql_fetch_array($StoredText))
    	{
    		$SQL="Select * From Texts Where TemplateID = ".$Info['TemplateID']." and TextID = ".$Text['TextID'];
    		$LayoutValues = mysql_query($SQL);
    		$Values = mysql_fetch_array($LayoutValues);
    		
    		//Get Colors
    		$Color = $Values['Color'];
    
    
    		//Assign Colors
    		$SQL="Select * From Colors Where Name = '$Color'";
    		$ColorRGB = mysql_query($SQL);
    		$RGB = mysql_fetch_array($ColorRGB);
    		$ShadowColor = imagecolorallocate($overlay, 160, 160, 160);
    		$TextColor = imagecolorallocate($overlay, $RGB['Red'], $RGB['Green'], $RGB['Blue']);
    
    		//Get Positioning Data for Text
    		$PosLeft = $Values['Left']*$Inch; 
    		$PosHeight  = $Values['Height']*$Inch;
    		$PosBottom = 864-($Values['Bottom']*$Inch);
    		$PosWidth  = $Values['Width']*$Inch;
    		$Font  = $Values['Font'];
    		$Shadow = $Values['Shadow'];
    		$FontSize = round($PosHeight);
    		$box = imagettfbbox ($FontSize, 0, $Font, $Text['Text']);
    		
    		//Size Text
    		while ($box[2] >= $PosWidth)
    			{
    				$FontSize = $FontSize-1;
    				$box = imagettfbbox ($FontSize, 0, $Font, $Text['Text']);
    			}
    		
    		//Center Text
    		$PosCenter = round($PosWidth - $box[2])/2;
    		
    //Set Destination for OverlayText Image 
    $TextOverlay = "Images/Text_Overlays/".$Info['ProjectID'].".png";
    
    		//Place Text
    		if ($Shadow == 'Yes')
    			{
    				//Shadow
    				$offset = intval($FontSize/10);
    				imagettftext($overlay,$FontSize, 0, $PosLeft + $PosCenter + $offset, $PosBottom + $offset, $ShadowColor , $Font , $Text['Text']);
    			}
    		//Text
    		imagettftext($overlay,$FontSize, 0, $PosLeft + $PosCenter, $PosBottom, $TextColor , $Font , $Text['Text']);
    }
    
    //Save TextOverlay
    imagesavealpha($overlay, true);
    imagepng($overlay,$TextOverlay);
    
    //Set Destination for Preview Image
    $Time = time();
    $preview = "Images/Previews/".$Info['ProjectID']."_".$Time.".png";
    $SQL="Update Projects Set ProjectPreview = '$preview' Where ClientID = $ClientID and Status = 'Current'";
    mysql_query($SQL);
    
    //Create Copy of Blank Project Template for Project Preview
    $ProjectPreview = imagecreatefrompng("Images/Project_Templates/".$Info['ProjectID'].".png");
    
    //Create Copy of Photo Overlay
    $PhotoOverlay = imagecreatefrompng("Images/Photo_Overlays/".$Info['ProjectID'].".png");
    
    //Create Copy of Text Overlay
    $TextOverlay = imagecreatefrompng("Images/Text_Overlays/".$Info['ProjectID'].".png");
    
    //Paste Photo Overlay
    imagecopy($ProjectPreview,$PhotoOverlay,0,0,0,0,570,864);
    
    //Paste Text Overlay 
    imagecopy($ProjectPreview,$TextOverlay,0,0,0,0,570,864);
    
    //Delete Previous Preview
    foreach (glob("Images/Previews/$Info[0]*.*") as $filename)
     {
    		unlink ($filename);
     }
     
    //Save Preview
    imagesavealpha($ProjectPreview, true);
    imagepng($ProjectPreview,$preview);
    echo $preview;
    ?>
    PHP:
    <?php
    $conn = mysql_connect("xxx","xxx","xxx"); 
    mysql_select_db("Brag_Flags",$conn);
    $ClientID = $_COOKIE["Brag_Flag_user"];
    $SQL="Select * From Projects Where ClientID = $ClientID and Status = 'Current'";
    $ProjectInfo = mysql_query($SQL);
    $Info = mysql_fetch_array($ProjectInfo);
    
    //Set Source of Uploaded Project Photo
    $image = $Info['ProjectPhoto'];
    
    //Create Copy of Original Uploaded Client Photo
    $org_img = imagecreatefromjpeg($image);
    
    //Get Dimensions of Uploaded Client Photo
    $imsize = getimagesize($image);
    
    //Scaling to match Cropper
    $Factor = $imsize[0]/300;
    
    //Scaled Dimensions of Crop
    $CropTop=$_GET["Top"]*$Factor;
    $CropLeft=$_GET["Left"]*$Factor;
    $CropHeight=$_GET["Height"]*$Factor;
    $CropWidth=$_GET["Width"]*$Factor;
    
    //Set Destination for Cropped Image
    $cropimage = "Images/Cropped_Images/".$Info['ProjectID'].".jpg";
    
    //Create Container for Cropped Image
    $img = imagecreatetruecolor($CropWidth,$CropHeight);
    
    
    //Create Cropped Image
    imagecopy($img,$org_img, 0, 0,$CropLeft,$CropTop, $CropWidth, $CropHeight);
    
    //Save Cropped Image
    imagejpeg($img,$cropimage);
    
    /****************************** Cropping Done Start Create Overlay for Preview ***************************/
    //Scaling to match Preview
    $Inch = 24;
    
    //Get Positioning Data for Photo
    $SQL="Select * From Photos Where TemplateID = ".$Info['TemplateID'];
    $PositionData = mysql_query($SQL);
    $Data = mysql_fetch_array($PositionData);
    $PosLeft = $Data[2]*$Inch;
    $PosWidth = $Data[4]*$Inch;
    $PosHeight = $Data[5]*$Inch;
    $PosTop = 864-($Data[3]*$Inch)-$PosHeight;
    
    //Set Destination for OverlayPhoto Image
    $PhotoOverlay ="Images/Photo_Overlays/".$Info['ProjectID'].".png";
    
    //Create Photo Overlay
    $overlay = imagecreatefrompng("Images/Overlay_Blanks/24x36.png");
    
    //Place Photo
    imagecopyresized($overlay, $img, $PosLeft, $PosTop, 0, 0, $PosWidth, $PosHeight, $CropWidth, $CropHeight);
    
    //Save PhotoOverlay
    imagesavealpha($overlay, true);
    imagepng($overlay,$PhotoOverlay);
    
    //Set Destination for Preview Image
    $Time = time();
    $preview = "Images/Previews/".$Info['ProjectID']."_".$Time.".png";
    $SQL="Update Projects Set ProjectPreview = '$preview' Where ClientID = $ClientID and Status = 'Current'";
    mysql_query($SQL);
    
    //Create Copy of Project Template for Project Preview
    $ProjectPreview = imagecreatefrompng("Images/Project_Templates/".$Info['ProjectID'].".png");
    
    //Create Copy of Photo Overlay
    $PhotoOverlay = imagecreatefrompng("Images/Photo_Overlays/".$Info['ProjectID'].".png");
    
    //Create Copy of Text Overlay
    $TextOverlay = imagecreatefrompng("Images/Text_Overlays/".$Info['ProjectID'].".png");
    
    //Paste Text Overlay 
    imagecopy($ProjectPreview,$TextOverlay,0,0,0,0,570,864);
    
    //Paste Photo Overlay
    imagecopy($ProjectPreview,$PhotoOverlay,0,0,0,0,570,864);
    
    //Delete Previous Preview
    foreach (glob("Images/Previews/$Info[0]*.*") as $filename)
     {
    		unlink ($filename);
     }
     
    //Save Preview
    imagesavealpha($ProjectPreview, true);
    imagepng($ProjectPreview,$preview);
    echo $preview;
    ?>
    PHP:
    Notice that the last part of the 2 scripts is identical, this is where the "overlays" are applied to the Template image. in the case of the Welcome banner the only thing different is that it contains no photo so the photo update script is never called but the photo overlay (which is a blank transparency) is applied in the text update script and vice versa. This is because the preview is created fresh each time a change is made and it is saved with a new filename (timestamp) so that client does not display cached image.

    The images are being created properly, I can eyeball them, however the preview has no "overlays" but the script does not generate and error!
     
    Sleeping Troll, Oct 1, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Try to put this all into one script, and then create functions to create the 'images' maby this works.
     
    EricBruggema, Oct 3, 2008 IP