Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Resize Image from Query

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1320 Views
  • fordy
    Avatar
    Community Member
    46 Posts

    Resize Image from Query Link to this post

    Hey,

    I am trying to resize an image that i am getting from a complex query. I have tried a few ways but not been successful. This is what i have at the moment. I cant get it to work. Is there any other way?

    PHP:

    function ShowHomeVacancies(){
       
           $sqlQuery = new SQLQuery();
             $sqlQuery->select = array(
              'Vacancy.*',
              'VacancyCategory.vCategory As vCategoryName',
              'SiteTree.URLSegment As vURLSegment',
             '(SELECT Member.Company FROM Member Where Member.ID= Vacancy.MyEmployerID) AS Employer',
             '(SELECT Member.MemberLogoID FROM Member Where Member.ID= Vacancy.MyEmployerID) AS EmployerLogo',
             '(SELECT Filename FROM File LEFT JOIN Member ON File.ID = Member.MemberLogoID WHERE Member.ID= Vacancy.MyEmployerID) AS Logo',
             'Vacancy.hasLogo AS hasLogo'
             );
             $sqlQuery->from = array(
                "Vacancy",
                "LEFT JOIN VacancyCategory ON Vacancy.MyCategoryID = VacancyCategory.ID",
                "LEFT JOIN VacancyCategoryPage ON Vacancy.MyCategoryID = VacancyCategoryPage.vCategoryID ",
                "LEFT JOIN SiteTree ON VacancyCategoryPage.ID = SiteTree.ID"
             );
             
             
             $sqlQuery->where = array(
                "vActive = true "
             );
          
             //$sqlQuery->orderby = "Vacancy.ID DESC";   
                
             $sqlQuery->limit = "0, 5";
             
             $result = $sqlQuery->execute();

             $pagedVacancies = singleton('Vacancy')->buildDataObjectSet($result);
             
             return $pagedVacancies;
          
       }
       
       function eLogo($ID){
          
          return ($img = DataObject::get_by_id("Image", $ID) ? $img : false);
          
       }

    SS Code:

    <% if ShowHomeVacancies %>
       
       <% control ShowHomeVacancies %>
          
          <div class="VacancyListItem $EvenOdd">
             <% if hasLogo = 1 %>
                   
                               
       <% control eLogo($EmployerLogo).SetWidth(80) %>
       <img src="$URL" alt="Company Logo" />
       <% end_control %>

                
             <% end_if %>
             <h2><a href="/$vURLSegment/show/$ID">$vTitle</a></h2>
             <p><span class="bold">Location: </span>$vLocation</p>
             <p><span class="bold">Salary: </span>$vSalary</p>
             <p>$vDescription.LimitWordCount(30)</p>   
             <p><span class="bold">Sector: </span><a href="/$vURLSegment/">$vCategoryName</a></p>
          </div>   
             
    <% end_control %>

    <% end_if %>

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Resize Image from Query Link to this post

    I dont think you can call eLogo() and pass variables like

    <% control eLogo($EmployerLogo).SetWidth(80) %>
    <img src="$URL" alt="Company Logo" />
    <% end_control %>

    what I would do is in the ShowHomeVacancies method down the bottom is do something like this (Note doing this completely off the top of my head)

    ....
    $result = $sqlQuery->execute();
    $pagedVacancies = singleton('Vacancy')->buildDataObjectSet($result);
    foreach($pagedVacancies as $vacancie) {
    $vacancie->Logo = $this->eLogo($vacancie->EmployerLogo;
    }
    return $pagedVacancies;
    ...

    Then in the template just do a control Logo. Im not sure if the above works, if it saves the Logo field on the dataobject. You might just need to play around with that

  • fordy
    Avatar
    Community Member
    46 Posts

    Re: Resize Image from Query Link to this post

    IT WORKED!

    Super, thanks willr. I had been scratching my head on this one for ages.

    I played about with my code but it is basically the same. Here is my final code incase anyone else wants it.

    PHP:

    function ShowHomeVacancies(){
       
           $sqlQuery = new SQLQuery();
             $sqlQuery->select = array(
              'Vacancy.*',
              'VacancyCategory.vCategory As vCategoryName',
              'SiteTree.URLSegment As vURLSegment',
             '(SELECT Member.Company FROM Member Where Member.ID= Vacancy.MyEmployerID) AS Employer',
             '(SELECT Member.MemberLogoID FROM Member Where Member.ID= Vacancy.MyEmployerID) AS EmployerLogo',
             'Vacancy.hasLogo AS hasLogo'
             );
             $sqlQuery->from = array(
                "Vacancy",
                "LEFT JOIN VacancyCategory ON Vacancy.MyCategoryID = VacancyCategory.ID",
                "LEFT JOIN VacancyCategoryPage ON Vacancy.MyCategoryID = VacancyCategoryPage.vCategoryID ",
                "LEFT JOIN SiteTree ON VacancyCategoryPage.ID = SiteTree.ID"
             );
             
             
             $sqlQuery->where = array(
                "vActive = true "
             );
          
             $sqlQuery->orderby = "Vacancy.ID DESC";   
                
             $sqlQuery->limit = "0, 5";

             $result = $sqlQuery->execute();

             $pagedVacancies = singleton('Vacancy')->buildDataObjectSet($result);
             
             foreach($pagedVacancies as $vacancie) {
                if ($vacancie->EmployerLogo != 0 && $vacancie->hasLogo == true){
                   $vacancie->Logo = $this->eLogo($vacancie->EmployerLogo);
                }
             }
             
             return $pagedVacancies;
          
       }
       
       function eLogo($ID){
                
          return ($LogoImg = DataObject::get_by_id("Image", $ID)) ? $LogoImg : false;
          
       }

    SS Code:

    <% if ShowHomeVacancies %>
       
       <% control ShowHomeVacancies %>
          
          <div class="VacancyListItem $EvenOdd">
             <% if hasLogo = 1 %>
                <% control Logo.SetWidth(120) %>
                <img src="$URL" alt="Company Logo" />
                <% end_control %>
             <% end_if %>
             <h2><a href="/$vURLSegment/show/$ID">$vTitle</a></h2>
             <p><span class="bold">Location: </span>$vLocation</p>
             <p><span class="bold">Salary: </span>$vSalary</p>
             <p>$vDescription.LimitWordCount(30)</p>   
             <p><span class="bold">Sector: </span><a href="/$vURLSegment/">$vCategoryName</a></p>
          </div>   
             
    <% end_control %>

    <% end_if %>

    1320 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.