Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6540041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:55:51+00:00 2026-05-25T10:55:51+00:00

I’m currently porting a pretty basic gallery application from PHP to Go. This application

  • 0

I’m currently porting a pretty basic gallery application from PHP to Go. This application features automatic generation of thumbnails and middle-sized version of every image.

In PHP I used GD, because it ships with it and worked pretty well. (Code is at the end of the question). I thought I could just replicate that in Go and found go-gd from https://github.com/bolknote/go-gd (again, code is at the end). It works, but it is roughly 10 times slower (measured using time wget $URL). The PHP implementation takes about 1 second for generating a 1024×768 version from a 10 MP-image, while the Go-Code takes almost 10 seconds.

Is there any way to speed this up or any other image-processing libary for Go, which implements scaling and convolution while being reasonably fast?

PHP-Code

public function saveThumb($outName, $options) {
    $this->img = imagecreatefromjpeg($filename);
    if (!is_dir(dirname($outName))) {
        mkdir(dirname($outName), 0777, true);
    }

    $width = imagesx($this->img);
    $height = imagesy($this->img);

    if ($options["keep_aspect"]) {
        $factor = min($options["size_x"]/$width, $options["size_y"]/$height);
        $new_width = round($factor*$width);
        $new_height = round($factor*$height);
    } else {
        $new_width  = $options["size_x"];
        $new_height = $options["size_y"];
    }

    // create a new temporary image
    $tmp_img = imagecreatetruecolor($new_width, $new_height);

    // copy and resize old image into new image
    imagecopyresampled($tmp_img, $this->img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    if ($options["sharpen"]) {
        // define the sharpen matrix
        $sharpen = array(
            array(-1, -1.7, -1),
            array(-1.7, 20, -1.7),
            array(-1, -1.7, -1) 
        );

        // calculate the sharpen divisor
        $divisor = array_sum(array_map('array_sum', $sharpen));

        // apply the matrix
        imageconvolution($tmp_img, $sharpen, $divisor, 0);
    }

    // save thumbnail into a file
    imagejpeg($tmp_img, $outName);     
}

Go-Code

func (entry *entry) GenerateThumb(options ImageType, overwrite bool) os.Error {
    targetFilename := entry.Filename(imageType)
    sourceFilename := entry.Filename(IMAGE_TYPE_FULL)
    targetDirname, _ := filepath.Split(targetFilename)
    os.MkdirAll(targetDirname, 0777)

    targetFi, errT := os.Stat(targetFilename)
    sourceFi, errS := os.Stat(sourceFilename)

    image := gd.CreateFromJpeg(sourceFilename)
    if image == nil {
        return os.NewError("Image could not be loaded")
    }

    var targetX, targetY int = 0, 0

    if options.KeepAspect {
        factor := math.Fmin(float64(options.SizeX)/float64(image.Sx()), float64(options.SizeY)/float64(image.Sy()))
        targetX = int(factor*float64(image.Sx()))
        targetY = int(factor*float64(image.Sy()))
    } else {
        targetX = options.SizeX
        targetY = options.SizeY
    }
    tmpImage := gd.CreateTrueColor(targetX, targetY)
    image.CopyResampled(tmpImage, 0, 0, 0, 0, tmpImage.Sx(), tmpImage.Sy(), image.Sx(), image.Sy())

    if options.Sharpen {
        sharpenMatrix := [3][3]float32{
        {-1, -1.7, -1},
        {-1.7, 20, -1.7},
        {-1, -1.7, -1} }
        tmpImage.Convolution(sharpenMatrix, 9.2, 0)
    }
    tmpImage.Jpeg(targetFilename, 90)

    return nil
}

EDIT: Go-Code using resize.go (see answer)

func (entry *entry) GenerateThumb(options ImageType, overwrite bool) os.Error {
    targetFilename := entry.Filename(imageType)
    sourceFilename := entry.Filename(IMAGE_TYPE_FULL)
    targetDirname, _ := filepath.Split(targetFilename)
    os.MkdirAll(targetDirname, 0777)

    targetFi, errT := os.Stat(targetFilename)
    sourceFi, errS := os.Stat(sourceFilename)

    if errT == nil && errS == nil {
        if targetFi.Mtime_ns > sourceFi.Mtime_ns && !overwrite {
            // already up-to-date, nothing to do
            return nil
        }
    }

    log.Printf("Generate(\"%v\", %v)\n", imageType, overwrite)

    inFile, fErr := os.Open(sourceFilename)
    if fErr != nil {
        log.Fatal(fErr)
    }
    defer inFile.Close()

    img, _, err := image.Decode(inFile)
    if err != nil {
        log.Fatal(err)
    }

    var targetX, targetY int
    if options.KeepAspect {
        factor := math.Fmin(float64(options.SizeX)/float64(img.Bounds().Max.X), float64(options.SizeY)/float64(img.Bounds().Max.Y))
        targetX = int(factor*float64(img.Bounds().Max.X))
        targetY = int(factor*float64(img.Bounds().Max.Y))
    } else {
        targetX = curType.SizeX
        targetY = curType.SizeY
    }
    newImg := resize.Resample(img, image.Rect(0, 0, img.Bounds().Max.X, img.Bounds().Max.Y), targetX, targetY)

    var outFile *os.File
    outFile, fErr = os.Create(targetFilename)
    if fErr != nil {
        log.Fatal(fErr)
    }
    defer outFile.Close()

    err = jpeg.Encode(outFile, newImg, &jpeg.Options{90})
    if err != nil {
        log.Fatal(err)
    }
    return nil
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:55:52+00:00Added an answer on May 25, 2026 at 10:55 am

    The Moustachio example application for GAE by Andrew Gerrand contains a resize.go file with a native Go implementation. There was also a similar question on the go-nuts mailing list some days ago and Nigel has posted an updated version of this file there. You might want to try it 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.