Magnetic fields can be generated during an inflationary phase in the early universe by amplification of perturbations of the electromagnetic field. The resulting magnetic fields can have interesting field strengths if the conformal invariance of Maxwell's equations is broken. Two particular models are discussed, one based on extra dimensions and one on nonlinear electrodynamics.